Sunday, July 12, 2009

How do you get a user to input a list of numbers (UNKNOWN how many) into an array using C++??

I had posted a question similar to this, but realized that one thing was missing.


Here is what is supposed to go on.


-Ask user for a list of numbers


-When the user inputs "-1" cause a break to happen and end loop


-Store the numbers entered into an array.


-State whether each number listed is above or below the average of the numbers in the array.





I am using C++ and Quincy2005





My question now is HOW do I make an array if I do not know how many numbers will be entered yet????





Before i realized the array is needed for an unknown number, I used 5.. but this is wrong. Please let me know HOW to make an array like this.





So far, this is what I have (with an array[4], which is WRONG):


{


int i, sum, average;





int grades[4];





for (i=0; i%26lt;5; i++)


{


cin %26gt;%26gt; grades[i];


}





for (i=0; i%26lt;5; i++)


{


sum = grades[0] + grades[1] + grades[2] + grades[3] + grades[4];


}





average = sum / 5;





cout %26lt;%26lt; grades[0];


if (grades[0] %26gt;= average)


{





if (grades[0] %26gt; average)


{cout %26lt;%26lt;

How do you get a user to input a list of numbers (UNKNOWN how many) into an array using C++??
The easiest way to do this to to think about the maximum number of numbers the user might input and assign that as the size of the array. If you don't want to do this, you have to use a dynamic array or a linked list, which might be a little complicated for you because you have to create classes and I am not sure if you know how to do that.





Even if you use dynamic arrays your code is going to be a little complicated because you have to resize the array everytime the user enters more numbers than you allocated.





I suggest you create an array of 100 integers (or more if you think the user might input more than that, but 100 sounds good to me). Here is how you would do that:





#include %26lt;iostream%26gt;





using namespace std;





int main()


{


const int MAX_NUMS = 100;


const int DONE = -1;


int grades[MAX_NUMS];


int count = 0;


int userInput = 0;


int sum = 0;


double average = 0.0;





// Get the user's input


while(userInput != DONE %26amp;%26amp; count %26lt; MAX_NUMS)


{


cout %26lt;%26lt; "Enter grade or (" %26lt;%26lt; DONE %26lt;%26lt; ") to exit: ";


cin %26gt;%26gt; userInput;


if(userInput != DONE)


{


grades[count] = userInput;


sum += userInput;


count++;


}


}





// Calculate the average


average = (double) sum / (double) count;





// State grade above / below average


for(int i = 0; i %26lt; count; i++)


{


cout %26lt;%26lt; "( " %26lt;%26lt; grades[i] %26lt;%26lt; ") is ";


if((double) grades[i] %26gt; average)


cout %26lt;%26lt; "above average" %26lt;%26lt; endl;


else if((double) grades[i] %26lt; average)


cout %26lt;%26lt; "below average" %26lt;%26lt; endl;


else


cout %26lt;%26lt; "equal to average" %26lt;%26lt; endl;


}





return 0;


}








Let me know if you need anymore help.
Reply:i think dynamic Array will solve the Problem.


Else declare for a large value.the remaing will be consumed.
Reply:You will have to use dynamic memory allocation to accomplish this. Since the array size is not static you will have to allocate memory every time the user enters a valid input(in your case the input is valid as long as it is not equal to -1). I wrote a small program to accomplish this. Take a look at it and you can fine-tune it according to your needs:











#include %26lt;iostream.h%26gt;





int main(){


int i;


int *p,*q;


//************************


// Assign the pointer p


//************************


p = new int;


//************************************


// Assign p to q


// Now p and q point to same location


//************************************


q = p;


//***********************


// Get the array first


//***********************


do {


cout %26lt;%26lt; "Enter number:" %26lt;%26lt; endl;


cin %26gt;%26gt; i;


if(i!=-1){


*p = i;


p++;


}


}while(i!=-1);





//***************************


// Now display the array


//***************************


cout %26lt;%26lt; "The array entered by the user is " %26lt;%26lt; endl ;


while(q!=p){


cout %26lt;%26lt; *q %26lt;%26lt; endl;


q++;


}


return 0;


}





Here is the output:


#=============================


Enter number:


12


Enter number:


13


Enter number:


15


Enter number:


2


Enter number:


-1


The array entered by the user is


12


13


15


2


#=============================

local florist

No comments:

Post a Comment