C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 2nd, 2007, 11:41 AM
#<Hspd7> #<Hspd7> is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 12 #<Hspd7> User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 13 m 2 sec
Reputation Power: 0
Help with arrays

my code:

Code:
#include <cmath>
#include <iostream>
using namespace std;

int main()
{
    const int arraySize = 15;
    int a[arraySize];
    
    int total = 0;
    
    cout << "This program will take numbers that you enter and add subsequent\n"
         << "numbers, in which you enter, to the value you entered prior, then\n" 
         << "it will display their sum."<<endl;
    cout <<"\n";     
    cout <<" Enter a number: ";
    
    for(int i = 0; i < arraySize; i++)
            {
                cin >> a[arraySize];
                total += a[arraySize];
                cout <<"\nEnter another number"<<endl;
               
            }
            cout <<"\n";
            cout << "Your total is " <<total<<" Thank you" <<endl;
            cout <<"\n";
system("PAUSE");
return 0;
}


the program works ok, what I'm trying to understand is... what "i" stand for if arraySize sets the element size... couldn't I just as well say for(int arraySize = 0; arraySize<15; arraySize++)?

any help would be much appreciated, I'm new to programming so don't be surprised I'd ask such a dumb question, as I'm sure it is lol. Thanks in advance.

Reply With Quote
  #2  
Old January 2nd, 2007, 12:22 PM
costas costas is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 407 costas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 3 h 25 m 24 sec
Reputation Power: 12
What you've written should work, but not the way you've imagined! You should put where arraySize i! See:
Code:
for(int i=0;i<arraySize;i++)
{
cin>>a[i];
total += a[i];
cout<<"\nEnter another number"<<endl;
}

The way I'm doing looks like this:
first loop: i=0, total=0, then you get the value of the element 0 (i=0) of the array a!, then you add the value to total
second loop: i=1, total=the_previously_entered_value, then you get the value of the element 1 (i=1) of the array a!, then you add the value to total
AND SO ON
The way you're doing it looks like this:
first loop: i=0, total=0, then you get the value of the element 15 (arraySize=15) of the array a!, then you add this value to total
second loop: i=1, total=..., then you get the value of the element 15 again (arraySize=15 -> doesn't change) .....
AND SO ON

The way you're doing it, the only element of the array having value would be the 16th (15)! Make a loop to see that:
Code:
for(int i=0;i<=15;i++)
cout<<a[i]<<endl;

This would have an output like this:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
x (x = total)
-------------------------------------
Hope I Helped and Have a happy New Year
__________________
www.tosc.gr - Greek site with free programs to make your life easier
www.friendgr.gr - new torrent tracker - supports english...
Costas

Reply With Quote
  #3  
Old January 2nd, 2007, 12:34 PM
#<Hspd7> #<Hspd7> is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 12 #<Hspd7> User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 13 m 2 sec
Reputation Power: 0
awesome

awesome, thanks, that answered my question plus some.

Quote:
Originally Posted by costas
What you've written should work, but not the
way you've imagined! You should put where arraySize i! See:
Code:
for(int i=0;i<arraySize;i++)
{
cin>>a[i];
total += a[i];
cout<<"\nEnter another number"<<endl;
}

The way I'm doing looks like this:
first loop: i=0, total=0, then you get the value of the element 0 (i=0) of the array a!, then you add the value to total
second loop: i=1, total=the_previously_entered_value, then you get the value of the element 1 (i=1) of the array a!, then you add the value to total
AND SO ON
The way you're doing it looks like this:
first loop: i=0, total=0, then you get the value of the element 15 (arraySize=15) of the array a!, then you add this value to total
second loop: i=1, total=..., then you get the value of the element 15 again (arraySize=15 -> doesn't change) .....
AND SO ON

The way you're doing it, the only element of the array having value would be the 16th (15)! Make a loop to see that:
Code:
for(int i=0;i<=15;i++)
cout<<a[i]<<endl;

This would have an output like this:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
x (x = total)
-------------------------------------
Hope I Helped and Have a happy New Year

Reply With Quote
  #4  
Old January 2nd, 2007, 01:13 PM
costas costas is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Posts: 407 costas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 3 h 25 m 24 sec
Reputation Power: 12
Glad I helped!!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with arrays


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap