Adding Array Values

Adding Array Values

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1347 Views - Last Post: 14 January 2009 - 03:57 AM Rate Topic: -----

#1 ribena500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-January 09

Adding Array Values

Post icon  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

Is This A Good Question/Topic? 0
  • +

Replies To: Adding Array Values

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Adding Array Values

Posted 13 January 2009 - 05:20 AM

Gosh.. And this has an intermediate tag..
Was This Post Helpful? 0
  • +
  • -

#3 ribena500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-January 09

Re: Adding Array Values

Posted 13 January 2009 - 05:22 AM

View PostGloin, on 13 Jan, 2009 - 04:20 AM, said:

Gosh.. And this has an intermediate tag..


I TAKE IT YOU CAN HELP ME.
PLEASE HELP
Was This Post Helpful? 0
  • +
  • -

#4 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

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.
Was This Post Helpful? 0
  • +
  • -

#5 ribena500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-January 09

Re: Adding Array Values

Posted 13 January 2009 - 05:25 AM

View PostGloin, 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
Was This Post Helpful? 0
  • +
  • -

#6 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Adding Array Values

Posted 13 January 2009 - 05:26 AM

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

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



What do you represent with the array called hours_run?
Was This Post Helpful? 0
  • +
  • -

#7 ribena500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-January 09

Re: Adding Array Values

Posted 13 January 2009 - 05:28 AM

View PostGloin, on 13 Jan, 2009 - 04:26 AM, said:

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

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
Was This Post Helpful? 0
  • +
  • -

#8 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

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.

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.
Was This Post Helpful? 0
  • +
  • -

#9 ribena500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-January 09

Re: Adding Array Values

Posted 13 January 2009 - 05:47 AM

View PostGloin, 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.

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
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6033
  • View blog
  • Posts: 23,415
  • Joined: 23-August 08

Re: Adding Array Values

Posted 13 January 2009 - 05:50 AM

PROTIP: You don't have to print a variable every time it changes.
Was This Post Helpful? 0
  • +
  • -

#11 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Adding Array Values

Posted 13 January 2009 - 05:56 AM

View PostJackOfAllTrades, on 13 Jan, 2009 - 04:50 AM, said:

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


:genius:
What he said..
Was This Post Helpful? 0
  • +
  • -

#12 ribena500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-January 09

Re: Adding Array Values

Posted 13 January 2009 - 05:59 AM

View PostGloin, on 13 Jan, 2009 - 04:56 AM, said:

View PostJackOfAllTrades, on 13 Jan, 2009 - 04:50 AM, said:

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


:genius:
What he said..


i dont want to print it, all i want to day is to give me on sum figure
Was This Post Helpful? 0
  • +
  • -

#13 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Adding Array Values

Posted 13 January 2009 - 06:02 AM

View Postribena500, 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


In the above example it seems you only calculated the sum of the hours (or cal[i] = 1 for all i)
Was This Post Helpful? 0
  • +
  • -

#14 ribena500  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 13-January 09

Re: Adding Array Values

Posted 13 January 2009 - 06:02 AM

View Postribena500, on 13 Jan, 2009 - 04:59 AM, said:

View PostGloin, on 13 Jan, 2009 - 04:56 AM, said:

View PostJackOfAllTrades, on 13 Jan, 2009 - 04:50 AM, said:

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


:genius:
What he said..


i dont want to print it, all i want to day is to give me on sum figure


All i want it to say is you have run 9 hours.
I dont want it to print everything,
I cant seem to make this happen thiough
Was This Post Helpful? 0
  • +
  • -

#15 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,778
  • Joined: 20-September 08

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


#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();
}

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2