Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,179 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,453 people online right now. Registration is fast and FREE... Join Now!




Problems with for loop

 
Reply to this topicStart new topic

Problems with for loop

Alianin
post 7 Oct, 2008 - 05:42 PM
Post #1


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 7


My Contributions


I can't seem to find what the issue with my code is. I'm trying to calculate the sum of numbers from an array. Here is where I initialize the array:

CODE

    arrPrice[0] = .62;                //set amount for first element in price array
    
    for (int i = 1;i <= 19; i++)
    {
        arrPrice[i] = arrPrice[i-1] + .62;    //add .62 to previous price and set to new price
    }


And here is where I add it all up:

CODE

    sumPrice = 0;

    for (int i = 0;i <= 19; i++)
    {
        sumPrice = sumPrice + arrPrice[i];    //add price sum
    }


For some reason it's not adding correctly. It adds up correctly up to the 18th element and then when it hits 19 it just doubles the value of what 18 was. I hope I'm explaining this well enough.
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 7 Oct, 2008 - 08:53 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,012



Thanked 171 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Well I ran the code and appears to be working just fine. Here is my example and my output...

cpp

#include <iostream>
using namespace std;

int main() {
double arrPrice[20];
double sumPrice;

arrPrice[0] = .62; //set amount for first element in price array

for (int i = 1;i <= 19; i++)
{
arrPrice[i] = arrPrice[i-1] + .62; //add .62 to previous price and set to new price
cout << arrPrice[i] << endl;
}

sumPrice = 0;

for (int i = 0;i <= 19; i++)
{
sumPrice = sumPrice + arrPrice[i]; //add price sum
}

cout << "Sum is: " << sumPrice << endl;
return 0;
}


The output was....

CODE

1.24
1.86
2.48
3.1
3.72
4.34
4.96
5.58
6.2
6.82
7.44
8.06
8.68
9.3
9.92
10.54
11.16
11.78
12.4

Sum is: 130.2 (that is including the .62 at element 0)


So I am not sure what you are doing wrong. Run my test and see if you get the same results. If you do, obviously you are doing something wrong because I didn't change what you gave me other than putting in some output statements and making sure to define my variables as "double" (because you are handling decimals here).

Hope that helps. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Alianin
post 8 Oct, 2008 - 06:36 AM
Post #3


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 7


My Contributions


Wow. *slaps head* What a silly mistake to make. I declared my array 1 element short. Thanks for the brain jog Martyr!
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 03:43PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month