## 15 Replies - 1927 Views - Last Post: 14 January 2009 - 03:57 AM

### #1

# Adding Array Values

Posted 13 January 2009 - 05:13 AM

Hi,

I need to add together the values of an array that i have created,

my code:

#include "stdafx.h"

#include <iostream>

using namespace std;

int main ()

{

int i=0;

float hours_run[3], cal[3];

for(i=0; i<3; i++)

{

cout << "\n\Hours Run " << (i+1) << ": ";

cin >> hours_run[i];

cout << "calaories burnt " << (i+1) << ":";

cin >> cal[i];

}

cin.ignore(); cin.ignore();

}

I NEED TO ADD ALL THE VALUES FOR THE HOURS RUN, FOR EG IF THE HOURS RUN ARE 2 ,4,6 THEN I NEED IT TO DISPLAY 12 HOURS RUN

PLEASE HELP

I need to add together the values of an array that i have created,

my code:

#include "stdafx.h"

#include <iostream>

using namespace std;

int main ()

{

int i=0;

float hours_run[3], cal[3];

for(i=0; i<3; i++)

{

cout << "\n\Hours Run " << (i+1) << ": ";

cin >> hours_run[i];

cout << "calaories burnt " << (i+1) << ":";

cin >> cal[i];

}

cin.ignore(); cin.ignore();

}

I NEED TO ADD ALL THE VALUES FOR THE HOURS RUN, FOR EG IF THE HOURS RUN ARE 2 ,4,6 THEN I NEED IT TO DISPLAY 12 HOURS RUN

PLEASE HELP

##
**Replies To:** Adding Array Values

### #2

## Re: Adding Array Values

Posted 13 January 2009 - 05:20 AM

Gosh.. And this has an intermediate tag..

### #3

## Re: Adding Array Values

Posted 13 January 2009 - 05:22 AM

### #4

## Re: Adding Array Values

Posted 13 January 2009 - 05:23 AM

What is it that you need to sum up? I'm guessing the calories, so just use a variable, initiate it to 0. Loop over the values you need to sum up and add every value to the sum one by one. It's not more difficult than that.

### #5

## Re: Adding Array Values

Posted 13 January 2009 - 05:25 AM

Gloin, on 13 Jan, 2009 - 04:23 AM, said:

What is it that you need to sum up? I'm guessing the calories, so just use a variable, initiate it to 0. Loop over the values you need to sum up and add every value to the sum one by one. It's not more difficult than that.

IM NOT THAT EXPERIENCED, CAN YOU HELP WITH THE CODE? PLEASE

### #6

## Re: Adding Array Values

Posted 13 January 2009 - 05:26 AM

Ex: Sum all the values in the array cal[3]

What do you represent with the array called hours_run?

int sum = 0; for (j = 0; j < 3; j++) sum = sum + cal[j];

What do you represent with the array called hours_run?

### #7

## Re: Adding Array Values

Posted 13 January 2009 - 05:28 AM

Gloin, on 13 Jan, 2009 - 04:26 AM, said:

Ex: Sum all the values in the array cal[3]

What do you represent with the array called hours_run?

int sum = 0; for (j = 0; j < 3; j++) sum = sum + cal[j];

What do you represent with the array called hours_run?

THE NUMBER OF HOURS THAT THEY HAVE RUN,

I BASICALLY WANT IT TO SAY:

YOU 3 HAVE RUN A TOTAL OF x HOURS

AND BURNT A TOTAL OF x CALS

### #8

## Re: Adding Array Values

Posted 13 January 2009 - 05:43 AM

You can turn off the caps.

Ok.. So I'm assuming the calories burnt is represented as constant values meaning that if person1 and person2 each run for one hour they burn equal amount of calories. However, you burn different amount of calories every hour so running two hours mean you burn cal[0] + cal[1] calories.

For every person you can calculate the total number of burnt calories by first checking the number of hours the ran, then sum that many values from the cal array.

I don't think this is exactly what you wanted. I intentionally chose a different setup but the idea is the same. This is just so you get a feel of how you can do things and something to think about.

Ok.. So I'm assuming the calories burnt is represented as constant values meaning that if person1 and person2 each run for one hour they burn equal amount of calories. However, you burn different amount of calories every hour so running two hours mean you burn cal[0] + cal[1] calories.

For every person you can calculate the total number of burnt calories by first checking the number of hours the ran, then sum that many values from the cal array.

sum = 0; for (person = 0; person < 3; person++) { for (hour = 0; hour < hours_run[person]; hour++) sum = sum + cal[hour]; }

I don't think this is exactly what you wanted. I intentionally chose a different setup but the idea is the same. This is just so you get a feel of how you can do things and something to think about.

### #9

## Re: Adding Array Values

Posted 13 January 2009 - 05:47 AM

Gloin, on 13 Jan, 2009 - 04:43 AM, said:

You can turn off the caps.

Ok.. So I'm assuming the calories burnt is represented as constant values meaning that if person1 and person2 each run for one hour they burn equal amount of calories. However, you burn different amount of calories every hour so running two hours mean you burn cal[0] + cal[1] calories.

For every person you can calculate the total number of burnt calories by first checking the number of hours the ran, then sum that many values from the cal array.

I don't think this is exactly what you wanted. I intentionally chose a different setup but the idea is the same. This is just so you get a feel of how you can do things and something to think about.

Ok.. So I'm assuming the calories burnt is represented as constant values meaning that if person1 and person2 each run for one hour they burn equal amount of calories. However, you burn different amount of calories every hour so running two hours mean you burn cal[0] + cal[1] calories.

For every person you can calculate the total number of burnt calories by first checking the number of hours the ran, then sum that many values from the cal array.

sum = 0; for (person = 0; person < 3; person++) { for (hour = 0; hour < hours_run[person]; hour++) sum = sum + cal[hour]; }

I don't think this is exactly what you wanted. I intentionally chose a different setup but the idea is the same. This is just so you get a feel of how you can do things and something to think about.

Thank you,

When i use this code, i get something different.

for eg if i entered person 1 - 3 hours, person 2 - 2 hours and person 3 - 4 hours

I get

3

5

9

Can i just not get it straight to 9

### #10

## Re: Adding Array Values

Posted 13 January 2009 - 05:50 AM

PROTIP: You don't have to print a variable every time it changes.

### #11

## Re: Adding Array Values

Posted 13 January 2009 - 05:56 AM

### #12

## Re: Adding Array Values

Posted 13 January 2009 - 05:59 AM

### #13

## Re: Adding Array Values

Posted 13 January 2009 - 06:02 AM

ribena500, on 13 Jan, 2009 - 04:47 AM, said:

Thank you,

When i use this code, i get something different.

for eg if i entered person 1 - 3 hours, person 2 - 2 hours and person 3 - 4 hours

I get

3

5

9

Can i just not get it straight to 9

When i use this code, i get something different.

for eg if i entered person 1 - 3 hours, person 2 - 2 hours and person 3 - 4 hours

I get

3

5

9

Can i just not get it straight to 9

In the above example it seems you only calculated the sum of the hours (or cal[i] = 1 for all i)

### #14

### #15

## Re: Adding Array Values

Posted 14 January 2009 - 03:34 AM

Since you are using C++ why not use a vector of structs like below ...

Shalom,

David

http://developers-he...index.php/topic,46.0.html

Shalom,

David

http://developers-he...index.php/topic,46.0.html

#include <iostream> #include <vector> using namespace std; struct mylog { float hrs; float kcals; }; bool more() { cout << "More (y/n) ? "; cin.sync(); int reply=cin.get(); cin.sync(); return !(reply=='n' || reply=='N'); } int main() { vector < mylog > ml; mylog tmp; cout << "Enter data for hours and kcalories ...\n"; do { cout << "Enter hours : "; cin >> tmp.hrs; cout << "Enter kcals : "; cin >> tmp.kcals; if( !cin.good() ) { cout << "Bad data entered ... Enter numbers only\n"; cin.clear(); cin.sync(); continue; } ml.push_back( tmp ); }while( more() ); float tot_hrs =0, tot_kcals=0; for( unsigned i=0; i<ml.size(); ++i) { tot_hrs += ml[i].hrs; tot_kcals += ml[i].kcals; } cout << "\nTotal hours = " << tot_hrs << "\nTotal kcals = " << tot_kcals << "\n\nPress 'Enter' key to continue ... " << flush; cin.sync(); cin.get(); }