I'm not sure where I have fell off, but I am just not getting something (obviously something important). I have read several answers/questions, but have not figured out my problem to the problem.
Write a program that accepts input like the program in Display 7.8 and that outputs a bar graph like the one in that display EXCEPT that your program will output the bars vertically rather than horizontally. A two-dimensional array may be useful.
This is the 7.8 display, but it will not run on CodeBlocks although I typed it exactly from the book.
//Reads data and displays a bar graph showing productivity for each plant.
#include <iostream>
#include <cmath>
const int NUMBER_OF_PLANTS = 4;
void input_data(int a[], int last_plant_number);
void scale(int a[], int size);
void graph(const int asterisk_count[], int last_plant_number);
void get_total (int& sum);
int round(double number);
void print_asterisks (int n);
int main ()
{
using namespace std;
int production [NUMBER_OF_PLANTS];
cout<<"This program displays a graph showing\n"
<<"production for each plant in the company.\n";
input_data(production, NUMBER_OF_PLANTS);
scale(production, NUMBER_OF_PLANTS);
graph(production, NUMBER_OF_PLANTS);
return 0;
}
//Uses iostream:
void input_data(int a[], int last_plant_number)
{
using namespace std;
for (int plant_number = 1;
plant_number <= last_plant_number; plant_number ++)
{
cout<<endl;
<<"Enter production data for plant number "<<plant_number<<endl;
get_total(a[plant_number – 1]);
}
}
void get_total(int& sum)
{
using namespace std;
cout<<"Enter number of units produced by each department.\n"
<<"Append a negative number to the end of the list.\n";
sum = 0;
int next;
cin>>next;
while (next >= 0)
{
sum = sum + next;
cin>>next;
}
cout<<"Total = "<<sum<<endl;
}
void scale (int a[], int size)
{for (int index = 0; index < size; index++)
a[index] = round(a[index]/1000.0);
}
int round(double number)
{
using namespace std;
return static_cast<int>(floor (number + 0.5);
}
void graph (const int asterisk_count [], int last_plant_number)
{
using namespace std;
cout<< "units produced in thousands of units:\n";
for (int plant_number 1;
plant_number <= last_plant_number; plant_number++)
{
cout<<"Plant #"<<plant_number<<" ";
print_asterisks(asterisk_count[plant_number – 1]);
return 0;
}
}
Whatever is meant to print out as text needs to be enclosed by double quotes " ".
for example - if this is all text, then this....
1 2
cout<<This program displays a graph showing\n
<<production for each plant in the company.\n;
...should be like this
cout<<"This program displays a graph showing production for each plant in the company.\n";
If you can edit your post - highlight the code, then click on the <> button in the Format palette at the right side of the post, it'll format your code for the forum and make it a lot easier to read :)
#include <iostream>
#include <cmath>
constint NUMBER_OF_PLANTS = 4;
void input_data(int a[], int last_plant_number);
void scale(int a[], int size);
void graph(constint asterisk_count[], int last_plant_number);
void get_total (int& sum);
int round(double number);
void print_asterisks (int n);
int main ()
{
usingnamespace std;
int production [NUMBER_OF_PLANTS];
cout<<"This program displays a graph showing\n" <<"production for each plant in the company.\n";
input_data(production, NUMBER_OF_PLANTS);
scale(production, NUMBER_OF_PLANTS);
graph(production, NUMBER_OF_PLANTS);
return 0;
}
//Uses iostream:
void input_data(int a[], int last_plant_number)
{
usingnamespace std;
for (int plant_number = 1;plant_number <= last_plant_number; plant_number ++)
{
cout<<endl;
<<"Enter production data for plant number "<<plant_number<<endl;
get_total(a[plant_number – 1]);
}
}
void get_total(int& sum)
{
usingnamespace std;
cout<<"Enter number of units produced by each department.\n" <<"Append a negative number to the end of the list.\n";
sum = 0;
int next;
cin>>next;
while (next >= 0)
{
sum = sum + next;
cin>>next;
}
cout<<"Total = "<<sum<<endl;
}
void scale (int a[], int size)
{
for (int index = 0; index < size; index++)
a[index] = round(a[index]/1000.0);
}
int round(double number)
{
usingnamespace std;
returnstatic_cast<int>(floor (number + 0.5);
}
void graph (constint asterisk_count [], int last_plant_number)
{
usingnamespace std;
cout<< "units produced in thousands of units:\n";
for (int plant_number 1;plant_number <= last_plant_number; plant_number++)
{
cout<<"Plant #"<<plant_number<<" ";
print_asterisks(asterisk_count[plant_number – 1]);
return 0;
}
}
How did you do that? I before reposting, I copied from Codeblocks, thinking it would keep the format and coloring.
I am just going to drop out of school. If I can't figure this easy stuff out, there is no sense in even trying any longer.
Did you highlight the code before clicking the <> button? If you didn't, the button wouldn't do anything. Alternatively, you can just type the tags manually [ code ] before the code starts and [ / code ] after it ends. (remove the spaces inside the brackets)
You're missing the definition for the print_asterisks function, I don't know if just didn't get copied and pasted over.
You don't need a return 0 on line 67 since graph is a void function.
Line 63 is missing an =
round is actually the name of a function built in to C++, so I'd rename your round function to something else.
IN FUNCTION VOID_INPUT DATA (INT*, INT)
LINE 31 EXPECTED ] BEFORE NUMERIC CONSTANT
EXPECTED ) BEFORE NUMERIC CONSTANT
EXPECTED ; BEFORE ] TOKEN
int rnd(double number)
LINE 57 EXPECTED ) BEFORE ; TOKEN
void graph (const int*, INT)
LINE 66 EXPECTED ) BEFORE NUMERIC CONSTANT
EXPECTED ; BEFORE ] TOKEN
WHAT IS A "TOKEN"??
#include <iostream>
#include <cmath>
const int NUMBER_OF_PLANTS = 4;
void input_data(int a[], int last_plant_number);
void scale(int a[], int size);
void graph(const int asterisk_count[], int last_plant_number);
void get_total (int& sum);
int rnd(double number);
void print_asterisks (int n);
int main ()
{
using namespace std;
int production [NUMBER_OF_PLANTS];
cout<<"This program displays a graph showing\n" <<"production for each plant in the company.\n";
input_data(production, NUMBER_OF_PLANTS);
scale(production, NUMBER_OF_PLANTS);
graph(production, NUMBER_OF_PLANTS);
return 0;
}
//Uses iostream:
void input_data(int a[], int last_plant_number)
{
using namespace std;
for (int plant_number = 1;plant_number <= last_plant_number; plant_number ++)
{
cout<<endl;
cout<<"Enter production data for plant number "<<plant_number<<endl;
get_total(a[plant_number – 1]);
}
}
void get_total(int& sum)
{
using namespace std;
cout<<"Enter number of units produced by each department.\n" <<"Append a negative number to the end of the list.\n";
sum = 0;
int next;
cin>>next;
while (next >= 0)
{
sum = sum + next;
cin>>next;
}
cout<<"Total = "<<sum<<endl;
}
void scale (int a[], int size)
{
for (int index = 0; index < size; index++)
a[index] = rnd(a[index]/1000.0);
}
int rnd(double number)
{
using namespace std;
return static_cast<int>(floor (number + 0.5);
}
void graph (const int asterisk_count[], int last_plant_number)
{
using namespace std;
cout<< "units produced in thousands of units:\n";
for (int plant_number=1;plant_number <= last_plant_number; plant_number++)
{
cout<<"Plant #"<<plant_number<<" ";
print_asterisks(asterisk_count[plant_number – 1]);
}
}
If the minus sign in the lines that have this - [plant_number-1] got copied in from Word, it may not be the right character. I had to delete the characters in between the r in number and the 1 and replace the minus sign.
line 57 is missing a closing ) returnstatic_cast<int>(floor (number + 0.5);
You're calling the print_asterisks function on line 66 but it isn't defined anywhere that I can se..