Solved

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

Posted on 2011-02-25
20
340 Views
Last Modified: 2012-05-11
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
Comment
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
  • Learn & ask questions
  • 10
  • 10
20 Comments
 
LVL 4

Expert Comment

by:Alex Matzinger
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

by:HelpMeMaggi
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

by:
Alex Matzinger earned 500 total points
ID: 34983456
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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Comment

by:HelpMeMaggi
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

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

Author Comment

by:HelpMeMaggi
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

by:Alex Matzinger
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

by:HelpMeMaggi
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

by:Alex Matzinger
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;
            }
}

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
 

Author Comment

by:HelpMeMaggi
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

by:Alex Matzinger
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;
            }
}

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
 

Author Comment

by:HelpMeMaggi
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

by:Alex Matzinger
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;

Open in new window

0
 

Author Comment

by:HelpMeMaggi
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

by:Alex Matzinger
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

by:HelpMeMaggi
ID: 34984602
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
 
LVL 4

Expert Comment

by:Alex Matzinger
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

by:HelpMeMaggi
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

by:HelpMeMaggi
ID: 34986076
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
 
LVL 4

Expert Comment

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

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question