|
|
|
#1 (permalink) |
|
New to Overclock.net
|
I have this assignment and I have been able to declare an array but I need the information from the array to print(i.e user puts in 7 digits i need to list the 7 digits in a column. My code is below any assistance would be great.
#include <iostream> #include <math.h> using namespace std; int main() { int n, count; float x = 0, sum = 0, mean = 0, var = 0, sd = 0; cout << "How many numbers? "; cin >> n; int mainnum[n]; for (count = 1; count <= n; count++) { cout << "? "; cin >> x; mainnum[count-1] = n ; sum = sum + x; } // end for mean = sum / n; cout << "You have entered the following:" << endl; cout << mainnum[n] << "\r\n"; cout << "The sum is " << sum << endl; cout << "The meann is " << mean << endl; system("PAUSE"); return 0; } |
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
:wheee:
![]() |
Code:
int mainnum[n];
for (count = 1; count <= n; count++) {
cout << "? ";
cin >> x;
mainnum[count-1] = n
;
__________________
Woodwind instruments are "all your money are belong to us"ing my wallet.
$2,500+ for a half decent oboe? ![]()
|
|||||||||||||
|
|
|
|
#4 (permalink) | |||
|
New to Overclock.net
|
Quote:
The way you have it now is just filling up the array with the total number of digits that the user wants to input (the variable n). So instead it should be: Quote:
Quote:
To print it in a single column, you can pretty much reuse the same format as the for loop you created to fill the array. Instead of asking/inserting the digit into the array, just cout each array element followed by endl; Last edited by ocnjk : 09-18-09 at 10:26 PM |
|||
|
|
|
|
|
#5 (permalink) |
|
New to Overclock.net
|
That doesn't really do what I want either. I need the list to go below the portion of the program below.
#include <iostream> #include <math.h> using namespace std; int main() { int n, count; float x = 0, sum = 0, mean = 0, var = 0, sd = 0; cout << "How many numbers? "; cin >> n; int mainnum[n]; for (count = 1; count <= n; count++) { cout << "? "; cin >> x; mainnum[count-1] = n ; sum = sum + x; } // end for mean = sum / n; cout << "You have entered the following:" << endl; and the out put needs to look something like 12 34 32 65 87 54 |
|
|
|
|
|
#6 (permalink) |
|
New to Overclock.net
|
It actually does, maybe you aren't understanding what I wrote.
I'll be nice and give you the code (you add this at the end of the code after the "you have entered..." line). for(int i = 0; i < n; ++i){ cout << mainnum[i] << endl; } But I hope you can understand when using for loop with array, you should use initial counter at 0 instead of 1 and that you need to fix the part where you are inputting each number into the array. Last edited by ocnjk : 09-18-09 at 10:42 PM |
|
|
|
|
|
#8 (permalink) | |||||||||||||
|
Case Modder
![]() |
As already pointed out, you need to print each digit(more specifically each element of the array) individually. For example, you would first print mainnum[0], then print mainnum[1]... print mainnum[n-1]. Instead of writing seven separate print statements, you could print all the digits with a for loop similar to the one you used to get the input.
I'd also like to point out this line of code: mainnum[n] is invalid because the last element in the array has a length of n and is zero-indexed. mainnum[n] points to a space of memory after the last digit in your array, which you should not try to access. EDIT: His code is perfectly valid. Some people like to use an initial value of 1 and other people like to start from 0. Starting at 1 is easier for some people to understand and it can make the code easier to read(for some). There is no need to change it.
__________________
Interested in custom cases? Check out my scratch built acrylic case: BlackBox
Q6600 @ 4.32 Ghz 4 GHz Overclock Club ![]()
Last edited by jpz : 09-18-09 at 11:05 PM |
|||||||||||||
|
|
|
|
|
#10 (permalink) | |
|
New to Overclock.net
|
Quote:
Yes, it does work, but he is making the program execute an unnecessary operation. He may get away with it in a beginner's class, but it won't fly in more advanced classes (if you get a decent professor). It is better to get in the habit of properly coding early instead of later on. |
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|