Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 09-18-09   #1 (permalink)
New to Overclock.net
 
Join Date: Sep 2009
Posts: 18

Rep: 0 hsmith1976 Unknown
Unique Rep: 0
Trader Rating: 0
Default Using arrays in C++

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;
}
hsmith1976 is offline   Reply With Quote
Old 09-18-09   #2 (permalink)
:wheee:
 
mrtn400's Avatar
 
intel nvidia

Join Date: Aug 2007
Location: Carmichael, CA
Posts: 4,363
Blog Entries: 2

Rep: 279 mrtn400 is a proven membermrtn400 is a proven membermrtn400 is a proven member
Unique Rep: 237
Hardware Reviews: 1
Trader Rating: 1
Default

Code:
int mainnum[n];
    for (count = 1; count <= n; count++) {
        cout << "? ";
        cin >> x;
        mainnum[count-1] = n
        ;
You want the same thing as that, but opposite.
__________________
Woodwind instruments are "all your money are belong to us"ing my wallet.

$2,500+ for a half decent oboe?


System: Evolution (Alt Rig)
CPU
2x 3.06Ghz Xeon Prestonia (SL6VP) w/ HT off
Motherboard
MSI E7505 Master-LS2
Memory
4x 512MB Registered DDR-266
Graphics Card
BFG 7300GT 512MB @ 590Mhz Core/380Mhz Memory
Hard Drive
Hitachi 120GB
Sound Card
ASUS Xonar DX w/ shielding
Power Supply
450w
Case
Stock IBM
CPU cooling
Stock
GPU cooling
Stock
OS
Windows XP Professional SP3
Monitor
1x Acer 19" @ 1400x900, 1x Sceptre 19" @ 1280x1024
mrtn400 is offline Overclocked Account mrtn400's Gallery   Reply With Quote
Old 09-18-09   #3 (permalink)
New to Overclock.net
 
Join Date: Sep 2009
Posts: 18

Rep: 0 hsmith1976 Unknown
Unique Rep: 0
Trader Rating: 0
Default

Not sure what you mean by that or where to even put that in at.
hsmith1976 is offline   Reply With Quote
Old 09-18-09   #4 (permalink)
New to Overclock.net
 
Join Date: Apr 2008
Posts: 62

Rep: 7 ocnjk Unknown
Unique Rep: 6
Trader Rating: 0
Default

Quote:
Originally Posted by hsmith1976 View Post
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.

{
int mainnum[n];
for (count = 1; count <= n; count++) {
cout << "? ";
cin >> x;
mainnum[count-1] = n;
sum = sum + x;
}
i'm guessing you are trying to input each digit one by one into the mainnum array?

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:
mainnum[count] = x;
Also instead of doing [count-1] you can change the for loop to be the following so that you can use the line of code i wrote above.
Quote:
for (count = 0; count < n; ++count){

}
The reason is because arrays start with position 0. The loop will be performed while count is less than n. Let's say n is 3, the loop will run for count = 0, 1, 2.

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
ocnjk is offline   Reply With Quote
Old 09-18-09   #5 (permalink)
New to Overclock.net
 
Join Date: Sep 2009
Posts: 18

Rep: 0 hsmith1976 Unknown
Unique Rep: 0
Trader Rating: 0
Default

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
hsmith1976 is offline   Reply With Quote
Old 09-18-09   #6 (permalink)
New to Overclock.net
 
Join Date: Apr 2008
Posts: 62

Rep: 7 ocnjk Unknown
Unique Rep: 6
Trader Rating: 0
Default

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
ocnjk is offline   Reply With Quote
Old 09-18-09   #7 (permalink)
New to Overclock.net
 
Join Date: Sep 2009
Posts: 18

Rep: 0 hsmith1976 Unknown
Unique Rep: 0
Trader Rating: 0
Default

Thanks let me try this and I will get back with you.
hsmith1976 is offline   Reply With Quote
Old 09-18-09   #8 (permalink)
jpz
Case Modder
 
jpz's Avatar
 
intel ati

Join Date: Aug 2006
Location: Maryland
Posts: 931

Rep: 121 jpz is acknowledged by manyjpz is acknowledged by many
Unique Rep: 87
Trader Rating: 0
Default

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:

Quote:
Originally Posted by hsmith1976 View Post
cout << mainnum[n] << "\r\n";
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:

Quote:
Originally Posted by ocnjk View Post
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.
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

System: BlackBox
CPU
Q6600 3.6ghz @ 1.35V
Motherboard
Gigabyte X38-DS4
Memory
4GB Corsair Dominator 1033Mhz
Graphics Card
2x 3870 in Crossfire
Hard Drive
Hitachi P7K500 320GB
Sound Card
Creative X-Fi Titanium
Power Supply
Corsair 750TX
Case
BlackBox (scratch built)
CPU cooling
Apogee GTX
GPU cooling
2x Swiftech MCW-60 R2
OS
64-bit Gentoo Linux / 64-bit Vista Ultimate
Monitor
Sceptre X24-WG 24" WUXGA
Overclock.net Mod of the Month

Last edited by jpz : 09-18-09 at 11:05 PM
jpz is offline   Reply With Quote
Old 09-18-09   #9 (permalink)
New to Overclock.net
 
Join Date: Sep 2009
Posts: 18

Rep: 0 hsmith1976 Unknown
Unique Rep: 0
Trader Rating: 0
Default

Thanks for the information that part works now I just need to finish the program by adding the digits of the array together. Maybe back with you later for help with that but I am working on it.
hsmith1976 is offline   Reply With Quote
Old 09-18-09   #10 (permalink)
New to Overclock.net
 
Join Date: Apr 2008
Posts: 62

Rep: 7 ocnjk Unknown
Unique Rep: 6
Trader Rating: 0
Default

Quote:
Originally Posted by jpz View Post
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.
I never said it wasn't valid.

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.
ocnjk is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 03:59 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.15481 seconds with 8 queries