-
May 22nd, 2015, 04:31 PM
#1
Finding a total using for loops
Hello, I was just wondering if there was a way to code in a command that calculates a total number from the given bar chart I made. I'm using for loops. Thanks so much in advance guys!
Code:
int main()
{
cout << "Microbes Monthly Growth Char 100/cm" << endl;
cout << "***************************************" << endl;
int spec[5]; //array of from microbes from 5 specimen
int mic = 0;
for (int i = 0; i < 5; i++)
{
cout << "Specimen #" << i + 1 << endl;
cin >> mic;
spec[i] = mic / 100;
}
system("CLS");
for (int c = 0; c < 5; c++)
{
cout << "Specimen #" << c + 1;
for (int f = 0; f < spec[c]; f++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
-
May 23rd, 2015, 04:03 AM
#2
Re: Finding a total using for loops
Do you want the total of all the microbes displayed in the bar chart?
Code:
#include <iostream>
using namespace std;
const int arrsize = 5;
int main()
{
cout << "Microbes Monthly Growth Char 100/cm" << endl;
cout << "***************************************" << endl;
int spec[arrsize]; //array of from microbes from 5 specimen
int mic = 0;
for (int i = 0; i < arrsize; i++)
{
cout << "Specimen #" << i + 1 << endl;
cin >> mic;
spec[i] = mic / 100;
}
//system("CLS");
int total = 0;
for (int c = 0; c < arrsize; c++)
{
cout << "Specimen #" << c + 1;
for (int f = 0; f < spec[c]; f++)
cout << "*";
cout << endl;
total += spec[c];
}
cout << endl << "Total " << total << endl;
return 0;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 23rd, 2015, 04:29 PM
#3
Re: Finding a total using for loops
Hello, I have that down, user types in numbers then it goes to a screen clear and displays microbes through barchart. Was wondering if I could take all values and add them up as a total at the bottom. As in Total Microbes: #
-
May 23rd, 2015, 04:30 PM
#4
Re: Finding a total using for loops
Originally Posted by 2kaud
Do you want the total of all the microbes displayed in the bar chart?
Code:
#include <iostream>
using namespace std;
const int arrsize = 5;
int main()
{
cout << "Microbes Monthly Growth Char 100/cm" << endl;
cout << "***************************************" << endl;
int spec[arrsize]; //array of from microbes from 5 specimen
int mic = 0;
for (int i = 0; i < arrsize; i++)
{
cout << "Specimen #" << i + 1 << endl;
cin >> mic;
spec[i] = mic / 100;
}
//system("CLS");
int total = 0;
for (int c = 0; c < arrsize; c++)
{
cout << "Specimen #" << c + 1;
for (int f = 0; f < spec[c]; f++)
cout << "*";
cout << endl;
total += spec[c];
}
cout << endl << "Total " << total << endl;
return 0;
}
right, read that wrong, yes to your question, sorry.
-
May 26th, 2015, 10:07 AM
#5
Re: Finding a total using for loops
Originally Posted by 2kaud
Do you want the total of all the microbes displayed in the bar chart?
Apparently, you accumulate scaled down number (for display purposes?); see:
Code:
spec[i] = mic / 100;
I would calculate the total in the first loop, right after reading mic.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
May 29th, 2015, 07:31 AM
#6
Re: Finding a total using for loops
-
June 5th, 2015, 05:12 PM
#7
Re: Finding a total using for loops
thanks very much for responding, this helped me out tons!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|