Solved

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

Posted on 2011-02-25
20
331 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
  • 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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now