You probably would want to use an array of 10 integers to store the values and then use like a count variable to count how many times the user enters a number. Hope that helps. If you need more help let me know.
Well not the count variable in the for loop setup; although, you could probably do that. I was thinkging like a variable count that you increment every time. Sort of like this.
#include <iostream.h>
int main()
{
int count;
int iarray[10];
// Then put your code for prompting the user for the
// numbers
// Everytime they enter a number
count++;
// Then use the count variable to tell them how many
// numbers they entered
cout << "These are the numbers that you entered: " << endl;
for(int counts=0; counts<n; counts++)
{
cout << counts << " ";
}
cout << endl;
cout << "\nThe sum is " << sum << endl;
average = sum / n;
cout << "\nThe average is " << average << endl << endl;
return;
}//end main
This is my code so far but the problem is that when im supposed to display the values that were entered they don't print those out instead they display the numbers 0, 1, 2, 3 if I were to say enter only 4 numbers. Can you show me what I am doing wrong?
Are you wanting to just count to 10 or actually let them enter some numbers and you repeat them back to them? Cause right now it looks like you just looping from 0 - 9 and displaying the the sum of array garbage. When I say garbage I mean you don't have anything in that array, so it could be anything. Is that what you want to do? Or do you want the to enter like 54 and you tell them that's what they entered, and then tell them they entered 1 number? If you want to do that, put in a loop that prompts them for a number and store them in that x array. For example,
for(count = 0; count < n - 1; count++)
{
cout<< "Enter a number: " << endl;
cin<< x[count]; // that should store them in the array
}
The program sort of works now but I found that there is still one more problem. The problem is that I would like to restrict them from entering more than 10 numbers. But when i executed the program and typed in 11 numbers to enter it accepted it. I was wondering is there a way to make it so that if they do type in 11 or more that it will prompt the user that they have entered a number more than 10 and to please enter a number 10 or smaller.
#include <iostream>
using namespace std;
void main()
{
int n, count; //declaration
double sum=0, average;
int x[10]; //declare array’s elements as type int
cout << "How many numbers do you want to enter not exceeding 10? ";
cin >> n; //accept integer
for (count=0; count<=n-1; count++)
{
cout << "Enter number: " ; //print out enter number
cin >> x[count]; //accept integer
sum += x[count]; //assign integer to sum
cout << "These are the numbers that you entered: " << endl;
for(int counts=0; counts<=n-1; counts++)
{
cout << x[counts] << " "; //displays integers that were entered
}
cout << endl;
cout << "\nThe sum is " << sum << endl; //displays the sum of integers
average = sum / n; //calculates average of integers
cout << "\nThe average is " << average << endl << endl; //displays average
#include <iostream>
using namespace std;
void main()
{
int n, count; //declaration
double sum=0, average;
int x[10]; //declare array’s elements as type int
cout << "How many numbers do you want to enter not exceeding 10? ";
while(n > 10) // see if this fixes it
cout << "How many numbers do you want to enter not exceeding 10? ";
cin >> n; //accept integer
for (count=0; count<=n-1; count++)
{
cout << "Enter number: " ; //print out enter number
cin >> x[count]; //accept integer
sum += x[count]; //assign integer to sum
cout << "These are the numbers that you entered: " << endl;
for(int counts=0; counts<=n-1; counts++)
{
cout << x[counts] << " "; //displays integers that were entered
}
cout << endl;
cout << "\nThe sum is " << sum << endl; //displays the sum of integers
average = sum / n; //calculates average of integers
cout << "\nThe average is " << average << endl << endl; //displays average
int main()
{
int n, count; //declaration
double sum=0, average;
int x[10]; //declare array’s elements as type int
cout << "How many numbers do you want to enter not exceeding 10?";
cin >> n; //accept integer
while(n > 10){
cout << "How many numbers do you want to enter not exceeding 10?";
cin >> n;
}
for (count=0; count<=n-1; count++)
{
cout << "Enter number: " ; //print out enter number
cin >> x[count]; //accept integer
sum += x[count]; //assign integer to sum
cout << "These are the numbers that you entered: " << endl;
for(int counts=0; counts<=n-1; counts++)
{
cout << x[counts] << " "; //displays integers that were entered
}
cout << endl;
cout << "\nThe sum is " << sum << endl; //displays the sum of integers
average = sum / n; //calculates average of integers
cout << "\nThe average is " << average << endl << endl; //displays average
return 0;
}//end main
That should do it for ya. I compiled in VC++, and it seemed to work well. Have fun .
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.