Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

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

Posted on 2011-02-25
Medium Priority
342 Views
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.
0
Question by:HelpMeMaggi
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 10
• 10

LVL 4

Expert Comment

ID: 34983373
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

Author Comment

ID: 34983413
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

LVL 4

Accepted Solution

Alex Matzinger earned 2000 total points
ID: 34983456
to declare an integer array
``````int array[9];  //decalre the array
``````

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

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

post the code you currently have and i'll be able to help you out a little more
0

Author Comment

ID: 34983596
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

LVL 4

Expert Comment

ID: 34983645
change your array to  int and then post the output of a run through
0

Author Comment

ID: 34983711
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

LVL 4

Expert Comment

ID: 34983762
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

Author Comment

ID: 34983817
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

LVL 4

Expert Comment

ID: 34983867
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;
}
}
``````

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

Author Comment

ID: 34984084
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

LVL 4

Expert Comment

ID: 34984134
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;
}
}
``````

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;
}
}
``````
0

Author Comment

ID: 34984374
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

LVL 4

Expert Comment

ID: 34984388
yeah, the average would be something like:
``````float total = 0;
for(i = 0; i < 9; i++) {
total += array[i];
}

float average = total/9;
``````
0

Author Comment

ID: 34984532
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

LVL 4

Expert Comment

ID: 34984548
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

Author Comment

ID: 34984602
so i made a add++; in the loop but it gives me this huge number like 3234
why?
for  ( i = 0; i < 9; i++)
{           scanf("%f", &total);
array[ i ]  = total;
}
0

LVL 4

Expert Comment

ID: 34984630
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

Author Comment

ID: 34984702
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

Author Comment

ID: 34986076
The average kept coming out wrong.
float total = 0;
for(i = 0; i < 9; i++) {
total += array[i ];
}

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

LVL 4

Expert Comment

ID: 34989771
Try dividing by 9.0.  Sometimes c gets touchy when dividing floats by an integer
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to useâ€¦
Having just graduated from college and entered the workforce, I donâ€™t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back toâ€¦R. Â  So â€¦
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor anâ€¦
###### Suggested Courses
Course of the Month5 days, 8 hours left to enroll