# Thread: How would I display the results in a bar graph way?

1. ## How would I display the results in a bar graph way?

How would I display the results in a bar graph way?

Currently it does, depending on the count:

Code:
```*
*
*
*
*
*
*
*
*
*```
Code:
```    for(int i = 0; i < anspos1_count; i++)
{
cout << "*" << endl;

}

for(int j = 0; j < anspos2_count; j++)
{
cout << "\t *" << endl;
}

for(int k = 0; k < anspos3_count; k++)
{
cout << "\t \t *";
}```

2. You need to 'fuse' these loops into a single nested loop.
Here is a general solution you can play with and then adapt to your problem.
Code:
```#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> data={1,4,3,4,5,6,0,3};
int max = * std::max_element(data.begin(),data.end());
for(int i=max-1;i>=0;--i)
{
for(auto x:data)
std::cout<<(x>i?"*":" ")<<"\t";
std::cout<<std::endl;
}
return 0;
}```

3. Originally Posted by manasij7479
You need to 'fuse' these loops into a single nested loop.
Here is a general solution you can play with and then adapt to your problem.
Code:
```#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> data={1,4,3,4,5,6,0,3};
int max = * std::max_element(data.begin(),data.end());
for(int i=max-1;i>=0;--i)
{
for(auto x:data)
std::cout<<(x>i?"*":" ")<<"\t";
std::cout<<std::endl;
}
return 0;
}```

Hey, the following code you gave me is not executing, is giving errors.

Hey, the following code you gave me is not executing, is giving errors.
Compiler ? ..and error messages ?

5. Originally Posted by manasij7479
Compiler ? ..and error messages ?
Code:
```Error	1	error C2552: 'data' : non-aggregates cannot be initialized with initializer list	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	6
Error	2	error C2143: syntax error : missing ',' before ':'	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	10

Error	3	error C3531: 'x': a symbol whose type contains 'auto' must have an initializer	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	10

4	IntelliSense: initialization with '{...}' is not allowed for object of type "std::vector<int, std::allocator<int>>"	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	6

5	IntelliSense: cannot deduce 'auto' type (initializer required)	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	10

6	IntelliSense: expected a ';'	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	10

7	IntelliSense: expected an expression	c:\users\swadesh\c++ programs\pointerz\pointerz\pointerz.cpp	10```

6. Oops... either you need to tell your compiler to use C++11 features.. or they are not supported yet.

I'll let you figure out how to convert them to old C++.
(Terms to google for: Brace Enclosed Initializer List, Ranged For Loops)

7. Originally Posted by manasij7479
Oops... either you need to tell your compiler to use C++11 features.. or they are not supported yet.

I'll let you figure out how to convert them to old C++.
(Terms to google for: Brace Enclosed Initializer List, Ranged For Loops)
I am using Visual Studio 2010, can you tell me how I can use the C++11 features?

8. Maybe this will pass through your compiler
Code:
```#include <iostream>

int findMax(int*,const int);

int main()
{
int data[]={1,4,3,4,5,6,0,3};
int max = findMax(data,8);
for(int i=max-1;i>=0;--i)
{
for(int j = 0 ; j < 8 ; j++)
std::cout<<(data[j]>i?"*":" ")<<"\t";
std::cout<<std::endl;
}
return 0;
}

int findMax(int* data,const int n)
{
int max=data[0];
for(int i=1 ; i<n ; i++)
if(max<data[i])
max=data[i];
return max;
}```

I am using Visual Studio 2010
VS 2012 is available in MSDNAA if you are a student

10. So what you are trying to do is get manasij7479 to do your coding for you, eh? Wouldn't it be better to look at and study his code for understanding, then you can write the code that matches exactly into your program rather than having him keep changing his code until it fits your program?

Don't be so lazy. Learn to do from the examples.

11. Originally Posted by WaltP
So what you are trying to do is get manasij7479 to do your coding for you, eh? Wouldn't it be better to look at and study his code for understanding, then you can write the code that matches exactly into your program rather than having him keep changing his code until it fits your program?

Don't be so lazy. Learn to do from the examples.

Thats why I always post a C++11-ish version (which teachers probably won't accept for a few more years !).

Case 1: OP decides to learn C++11. Profit
Case 2: OP tries to understand the code and rewrites it himself.Profit.
Case 3: Someone comes along and hands out a vanilla solution. Loss.

12. Originally Posted by manasij7479

Thats why I always post a C++11-ish version (which teachers probably won't accept for a few more years !).
Just a thought...
Rather than the confusing syntax of pure C++11 (vectors, std:: et al) that must surely confuse someone that barely can do a FOR statement, how about psuedocode? They can see the steps but can't use the post as is. They have to think and translate into actual code.

13. Originally Posted by WaltP
Just a thought...
Rather than the confusing syntax of pure C++11 (vectors, std:: et al) that must surely confuse someone that barely can do a FOR statement, how about psuedocode? They can see the steps but can't use the post as is. They have to think and translate into actual code.
I always find pseudo-code (like the ones given in the Cormen book, which I'm reading now) very confusing in terms of translating ideas(maybe that is because I'm much more familiar with C++ than with, say python or pascal).

(Yesterday, I figured out Quick Sort's Partition function (and a variant of it provided for 'fun') only after seeing some C code on the web after an hour of looking at the pseudo code.)

14. Good pseudo code starts with a laundry list of tasks to do and an explanation of data. Then you just have to drill down until it looks like an implementation. I don't think programming experience has much to do with it. ALL of my formal education forced me to hand in program specifications before I wrote a program, which meant lots of pseudo code writing. I guess my education was unique that way.

15. Originally Posted by whiteflags
Good pseudo code starts with a laundry list of tasks to do and an explanation of data. Then you just have to drill down until it looks like an implementation.
But I'm talking about pseudo code to give an idea, not design from scratch toward implementation.

Originally Posted by manasij7479
I always find pseudo-code (like the ones given in the Cormen book, which I'm reading now) very confusing in terms of translating ideas(maybe that is because I'm much more familiar with C++ than with, say python or pascal).
So don't use that style of pseudo code.

std100093's code:
Code:
```int findMax(int* data,const int n)
{
int max=data[0];
for(int i=1 ; i<n ; i++)
if(max<data[i])
max=data[i];
return max;
}```
in pseudo code:
Code:
```function findMax(data [as array], n [as amount of data])
set max to first element of data
loop using I through the elements of data
test if max less than data[I]
if so replace max with data[I]
return max```
No code that can be directly copied
The OP must still think about the small details of the code (what loop to use; loop parameters)
They still learn with guidance, not get confused with gobbeldy-gook like
Code:
```    std::vector<int> data={1,4,3,4,5,6,0,3};  // they barely know what an array is, how is this syntax going to help them?
for(auto x:data)  // what is an auto? All I've learned is for(i=1; i<n; i++)
std::cout<<(x>i?"*":" ")<<"\t";  // what's this std thing? What's the ?"*":"" mean? Doesn't cout work?
// I've never seen anything like this in my like! I'm suppose to learn something from this?```
Target your audience. Don't show them neurosurgery when all they know is a band-aid.