Current time: 23 Apr 2024, 02:30 PM



c++ help
Offline Master

Registered User
Registered
11 Years of Service
Posts: 225
Threads: 124
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Feb 2013
Reputation: 0
Bronze Medal
#1
c++ help

i need help on how to get the sum of the array
#include <iostream>
using namespace std;
int main()


{

//Declarations
string name;
int input_num;
int high_num;
int low_num;
int counter;
int array[12];
//int average;
//int total = 0;
//Housekeeping
cout<<"Enter number or a negative number to QUIT"<<endl;

cin>>input_num;
high_num = input_num;
low_num = input_num;
counter = 0;


//MainLoop




while ((input_num > -1) && ( counter < 12))
{
array[counter] = input_num;
counter ++;
if (input_num > high_num)
high_num = input_num;

if( low_num > input_num)
low_num = input_num;

if ( counter < 12)
{

cout<<"Enter number or a negative number to QUIT"<<endl;

cin>>input_num;

}
}

for (int i = counter -1; i >= 0 ; i--)
cout<< array[i]<< " ";

cout<<endl;

//End of job()
if(counter > 0)
{
cout<<"Highest number entered "<<high_num<<endl;
cout<<"Lowest number entered "<<low_num<<endl;
cout<<"That's all folks!"<<endl;
}
else
cout<<"No numbers entered"<<endl;


return 0;
}




intro to c++


i have declare the total and the average but that's where i get lost its simple but i just cant get it, im not sure if i have to use a for statement. please help im confused, that's all i need to turn in my assignment


13 Apr 2016, 08:03 PM
Find Reply
Away SmG FlasH™

Senior Member
Senior Member
******
11 Years of Service
Posts: 3,129
Threads: 102
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Apr 2013
Reputation: 141
Location: Summoners Rift
Halo GodCrystal DonatorSmoothThe BombDiscord WarriorGavel of Trinity
DiamondGamerProgrammerThanks From Cloud
#2
Re: c++ help

Code:
int total = 0; //used to store the sum of the values in the array

for (int a = 0; a <= 12; a++) //use a for loop to access the values present in the array
    {
        if (array[a] < 0) //if the user enters a negative number (to exit program) it skips that value
        {
            continue;
        }
        cout << "Array Index " << a << "  Holds Value: " << array[a] << endl << endl; //prints out array index and the corresponding value
        total += array[a]; //total = total + the values in the array
        cout << "Total: " << total << endl; //display total
EDIT: If you have learnt functions, you should probably use them. Makes it really easy to understand and organized.
[Image: SPOILER_FlasH-1.gif]
14 Apr 2016, 05:20 AM
Find Reply
Offline SmG Shy

SmG Member
Member
*****
8 Years of Service
Posts: 227
Threads: 11
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Sep 2015
Reputation: 15
Halo God
#3
Re: c++ help

Flash teach me C++ too,ima pay you in cookies via fedx.
14 Apr 2016, 07:57 AM
Find Reply
Away SmG FlasH™

Senior Member
Senior Member
******
11 Years of Service
Posts: 3,129
Threads: 102
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Apr 2013
Reputation: 141
Location: Summoners Rift
Halo GodCrystal DonatorSmoothThe BombDiscord WarriorGavel of Trinity
DiamondGamerProgrammerThanks From Cloud
#4
Re: c++ help

SmG Shy Wrote:Flash teach me C++ too,ima pay you in cookies via fedx.

I'm still learning lol. What Master asked for was pretty basic.
[Image: SPOILER_FlasH-1.gif]
14 Apr 2016, 08:34 AM
Find Reply
Offline SmG Shy

SmG Member
Member
*****
8 Years of Service
Posts: 227
Threads: 11
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Sep 2015
Reputation: 15
Halo God
#5
Re: c++ help

Kappa I am offering you pakistani cookies...How can you be so cruel..

P.s Ima stop before I get mod queued for trolling.
14 Apr 2016, 03:26 PM
Find Reply
Offline SmG Dragonrage

Royal Dragon
Senior Member
******
11 Years of Service
Posts: 4,018
Threads: 206
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Aug 2012
Reputation: 38
Location: Atlantis
Gavel of TrinityLeague of LegendsProgrammerCoolCrystal DonatorLegendary
#6
Re: c++ help

flash, you gotta use to the code tag around ur code otherwise it gives you cancer trying to read it.
basically for total if all the values are in the array, and you want to add them all, you do this
Code:
int array[12] myArray;
//initialize your array here
int sum = 0;
for(int i = 0; i < 12; i++)
{
       sum += myArray[i];
}
cout<<"Total: "<<sum<<endl;

and for average just use
Code:
int array[12] myArray;
//initialize your array here
int sum = 0;
for(int i = 0; i < 12; i++)
{
       sum += myArray[i];
}
float avg = sum/12.0;
cout<<"Average: "<<avg<<endl;

instead, or you can output both if you want both.
[Image: mla7mw8hs3u0g9czg.jpg?size_id=5]
Do not meddle in the affairs of Dragons, for you are crunchy and taste good with ketchup!
14 Apr 2016, 03:37 PM
Website Find Reply
Offline Beasty

The Lord thy God
Senior Member
******
11 Years of Service
Posts: 7,654
Threads: 206
Likes Received: 14 in 11 posts
Likes Given: 0
Joined: Jun 2012
Reputation: 124
Location: Massachusetts
Discord: betsy#4548
WriterEmerald DonatorJokerOverwatch5KLegendary
Discord Warrior
#7
Re: c++ help

I, too, would like to learn Greek.
14 Apr 2016, 03:59 PM
Find Reply
Offline Master

Registered User
Registered
11 Years of Service
Posts: 225
Threads: 124
Likes Received: 0 in 0 posts
Likes Given: 0
Joined: Feb 2013
Reputation: 0
Bronze Medal
#8
Re: c++ help

[vaderz] [vaderz] thanks for the help i got it :D



please share [noez] sharing is caring, i made them :D but remember that "I am your father"

[Image: 59cd60b83a7a37165879597ef056b319.jpg]


14 Apr 2016, 08:01 PM
Find Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)
SmG Gaming, © 2010-2024
Theme By: «SmG» Cloud
Edited by: «SmG» Wires