How do I ask a user for 9 numbers then put them in an array.

I am trying to ask the user for nine numbers and then put them in an array. The program should stop tho if a negative number is inputed before he reaches the nine numbers then i would only want the array to keep the numbers he inputed. Can anyone help please.
HelpMeMaggiAsked:
Who is Participating?
 
Alex MatzingerConnect With a Mentor Database AdministratorCommented:
to declare an integer array
int array[9];  //decalre the array

Open in new window


to store the users data, use this type of staement
array[index] = user's input; //index goes from 0-8

Open in new window


to print it out use a for loop:
for(int i = 0; i < 9; i++)
    printf("%d\n", array[i])

Open in new window


post the code you currently have and i'll be able to help you out a little more
0
 
Alex MatzingerDatabase AdministratorCommented:
You need to use printf to ask the user and scanf to get the info from the user.  A simple if statement after each scanf to check the number.
0
 
HelpMeMaggiAuthor Commented:
I can get the the numbers from the user. But i dont know how to save them into an array to print out say array[3]; when i run it it gives me some random big number but i wanna get the number the user inputed.
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
HelpMeMaggiAuthor Commented:
I have declared my array as
 float array[9];
then i did
int i;
int total;
   for  ( i = 0; i < 9; i++)
{           scanf("%d", &total);          
    array[ i ]  = total;
}
 now say i want to print out the fourth number. how would i go about to do that?
i have tried printf("%d", array[3]); but it didn't work. or are the numbers inputed not being saved?
0
 
Alex MatzingerDatabase AdministratorCommented:
change your array to  int and then post the output of a run through
0
 
HelpMeMaggiAuthor Commented:
That worked!!! But the only problem now is that decimal numbers can be inputed that is why my array was a float array[9].
Now when i run it if i put a decimal in the program stops. I need to be able to include decimals.
0
 
Alex MatzingerDatabase AdministratorCommented:
ok, then change the array back to float and then you are going to use

float total;

scanf("%f", &total);  

and printf("%f", array[3]);  

C can get very picky with printing and scanf variables.  you have to make sure you use the right specifier
0
 
HelpMeMaggiAuthor Commented:
Okay that makes a lot of sense! Thank you!
You wouldn't happen to know how to find the biggest number that was inputed from the array?
I know that size is needed but how would i go about that.
I was told strl... something. but i have no clue.
0
 
Alex MatzingerDatabase AdministratorCommented:
You can do it when you input numbers

float maxNum 0;
intindex_of_max_num = 0;

for  ( i = 0; i < 9; i++)
{           scanf("%f", &total);          
            array[ i ]  = total;

            if(total > maxNum) {
                        maxNum = total;
                        index_of_max_num = i;
            }
}

Open in new window


this way you have the max number and the index of that number as well and it updates with eat number input into the program
0
 
HelpMeMaggiAuthor Commented:
what was the point of index_of_max_num = i????
so to get the minimum you would need

            if(total < maxNum) {
                        total = maxNum;
                        index_of_max_num = i;
            } ??????
0
 
Alex MatzingerDatabase AdministratorCommented:
index_of_max_num is just incase you wanted to keep track of the index of the maximum number, but it isn't needed for anything other than that.  

Yes, to get the minimum number that is the code that you would need.  Though you would have to set maxNum to be greater than 0 or else you won't get the correct results.  For min num use this:

float minNum;

for  ( i = 0; i < 9; i++)
{           scanf("%f", &total);          
            array[ i ]  = total;
            
             if(1 = 0) { //if this is the first number
                    minNum = total; //insure that minNum is actually set
             }  
             if(total < minNum) {
                        minNum = total;
            }
}

Open in new window


for both...use this

float minNum;
float maxNum = 0;

for  ( i = 0; i < 9; i++)
{           scanf("%f", &total);          
            array[ i ]  = total;
            
             if(1 = 0) { //if this is the first number
                    minNum = total; //insure that minNum is actually set
             }  
             if(total < minNum) {
                        minNum = total;
            }
             if(total > maxNum) {
                        maxNum = total;
            }
}

Open in new window

0
 
HelpMeMaggiAuthor Commented:
Nice i did it different but it worked. The average i cannot seem to figure out. I know you would have to add up all the numbers together and divided by what would be i, right?
0
 
Alex MatzingerDatabase AdministratorCommented:
yeah, the average would be something like:
float total = 0;
for(i = 0; i < 9; i++) {
       total += array[i];
}

float average = total/9;

Open in new window

0
 
HelpMeMaggiAuthor Commented:
it doesn't seem to be working.
Also say they only put in 7 numbers instead of 9 how would i average out only the numbers he inputed?
0
 
Alex MatzingerDatabase AdministratorCommented:
then you would have to keep an index of the numbers input and change the 9 to that value.  so lets say you keep a seperate integer called numEntries.  Everytime the user inputs a number, increment it, and then use it for your calculations when they are done entering numbers.  try dividing by 9.0 in the original case
0
 
HelpMeMaggiAuthor Commented:
so i made a add++; in the loop but it gives me this huge number like 3234
why?
int add;
for  ( i = 0; i < 9; i++)
{           scanf("%f", &total);          
            array[ i ]  = total;
            add++;
}
0
 
Alex MatzingerDatabase AdministratorCommented:
you need to make sure you initialize it to 0.  if you don't the c compiler will set it to whatever it feels like
0
 
HelpMeMaggiAuthor Commented:
Thank you so much! i still can't seem to get the average but i will work on it more and if not i will return for some more guide.
0
 
HelpMeMaggiAuthor Commented:
The average kept coming out wrong.
int add = 0;
float total = 0;
for(i = 0; i < 9; i++) {
       total += array[i ];
       add++;
}

float average = total/9;

why does the average not add up right, im using the basic numbers 1, 2,3, 4, 5, ,6 ,7, 8, 9 so i know the correct average.
0
 
Alex MatzingerDatabase AdministratorCommented:
Try dividing by 9.0.  Sometimes c gets touchy when dividing floats by an integer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.