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

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




Subsequent addition in an array

 
Reply to this topicStart new topic

Subsequent addition in an array

venom1
6 May, 2008 - 01:35 PM
Post #1

New D.I.C Head
*

Joined: 6 May, 2008
Posts: 2

CODE

#include <iostream>

using namespace std;

int main()
{
float tarray[10] = [5 2 6 8 6 1 2 3 4 1];
int numberJ = 10

cout <<"arrival rates: "<<endl;
    for (int i = 0; i<=numberJ; i++ )
    {
        float A[i];

      sum = sum + tarray[i];

      A[i] = sum;

      cout<<A[i]<<endl;
    }  
return 0;
}



This is a portion of code extracted from a larger body. The tarray is a set of inter-arrival times. I need to add
a term to the previous sum of all terms, recording each result in another array. I called this array, A.

The process is as follows:
tarray = [5 2 6 8 6 1 2 3 4 1]
add as explained above, record result: 5 + 2 = 7
7 + 6 = 13
13 + 8 = 21
21 + 6 = 27 and so on

Obtain new array: A = [7 13 21 27....]

Thanks for any help!
User is offlineProfile CardPM
+Quote Post

Whizzy
RE: Subsequent Addition In An Array
6 May, 2008 - 05:42 PM
Post #2

Unregistered





You pretty much nailed it... I made a few minor changes, and remarked them all (I think)... Using DEV C++ 4.9.9.2 it compiles and runs...

cpp
#include <iostream> 

using namespace std;

int main()
{
int A[10]; // moved this up here
int tarray[10] = {5,6,8,6,1,2,3,4,1}; //braces not brackets, add comma's
int numberJ = 10; // need semicolon
int sum=0; // need to initlize sum
cout <<"arrival rates: "<<endl;
for (int i = 0; i<=numberJ; i++ ){
// float A[i];
sum = sum + tarray[i];
A[i] = sum;
cout<<i<<". = "<<A[i]<<endl;
}
system("pause"); // my own preference
return 0;
}


+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:56PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month