Question:
How do I make an array in C++ programming?
?
2010-05-26 08:04:32 UTC
I have just started C programming as you most likely guessed from the question, but my booklet does not make a lot of sense on how to make an array. I am trying to make an average calculator with the array if that will help. Here is what I've got so far, and as you can see I pretty much have no idea what I'm doing.


#include

main()
{

float num[5], answer;

printf("Please enter 5 numbers to find their average: ");
scanf("%f", &num[0], num[0], num[0], num[0], num[0]);

answer = (num[0] + num[1] + num[2] + num[3] + num[4]) / 5;
printf("%f\n", answer);
}


I appreciate any help. Thanks.
Four answers:
knipp
2016-10-25 14:01:06 UTC
once you declare an array int array[5] then you're telling the compiler to placed aside 5 contiguous memory places which could each and each and every carry an int. you could placed characters into it yet printing it and utilising everyday string purposes gained't artwork. characters are only small integers -- in a lot of situations 0 to 255 variety. you could't placed a drift into it because the sizeof a drift isn't the same as an int. And the compiler has to understand what to do with the memory area. in case your array is of sort char then each and each and every memory area is one byte in length -- you could placed integers into it see you later as they are contained in the range of 0 to 255 (for unsigned char) or -127 to 127 for signed char. char records sort is an integer sort that continuously contains the ASCII personality set codes. An array is a contigiuos piece of memory that you'll keep issues in. all the products contained in the array want to be the same length so your programme can discover them An array index is used as an offset into the memory drift ar[10]; ar can carry ten floating aspect values -- the first being at memory area ar -- the finest at memory area ar+ 9*sizeof(drift) bytes from the position the array starts -- called ar subsequently. in case you particularly pick to keep diverse sorts of records in an array you should use an array of void * void * array[20]; it is an array of 20 addresses -- each and each and every deal with can aspect to a diverse sort of records. in view that addresses have the same length this may artwork -- the data they aspect to may have diverse sizes. to apply the data you should solid the price pointed to to the kind you pick. char c; .... c= (char) *array[10]; HTH
2010-05-27 03:51:51 UTC
You should use for loop so that it scanf the values easily and then you can printf it.



int i;

for(i=0;i<=4;i++)

{

scanf("%d", &num[i]);



}

it works for you surely..
2010-05-26 08:28:32 UTC
The problem lies in your scanf statement.



How is the user entering in 5 numbers? comma delimited? space delimited? You can't use %f to read in multiple numbers.



You could use a separate scanf statement for each number and set them to the appropriate array index.
Frecklefoot
2010-05-26 08:09:44 UTC
Well, does it work? If not, where are you having the problem? I think your best bet is to learn more about the language. See the link below. HTH


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...