Answer to Question #4820 in C++ for sidra

Question #4820
hey i need help in creating a program, this is the question :

Write a C++ program that uses a loop to read integers between 1 and 5 (inclusive) from the user. Once the loop terminates, the program prints a histogram of the entered values. Here is an example output:

1 *******
2 *
3 **********
4 ***
5 **

This means that the value 1 was entered 7 times and so forth.

and it should be done using counter controlled while loops
thanks in advance
1
Expert's answer
2011-10-25T08:28:57-0400
// Histogram of the entered values.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include "conio.h"
#include
<iostream>

using namespace std;
int _tmain(int argc, _TCHAR*
argv[])
{
int value;
int countfor1=0;
int countfor2=0;
int
countfor3=0;
int countfor4=0;
int countfor5=0;
int
arrayvalue[1000];
cout<<"Enter
value:";
for(;;){
cin>>value;
if(value>=1 &&
value<=5){
if(value==1){
countfor1++;

}
if(value==2){
countfor2++;
}
if(value==3){
countfor3++;

}
if(value==4){
countfor4++;
}
if(value==5){
countfor5++;

}
}else{
break;
}
}
char str1[80];
strcpy
(str1,"");
for(int i=0;i<countfor1;i++){
strcat
(str1,"*");
}

char str2[80];
strcpy (str2,"");
for(int
i=0;i<countfor2;i++){
strcat (str2,"*");
}
char str3[80];
strcpy
(str3,"");
for(int i=0;i<countfor3;i++){
strcat
(str3,"*");
}

char str4[80];
strcpy (str4,"");
for(int
i=0;i<countfor4;i++){
strcat (str4,"*");
}
char str5[80];
strcpy
(str5,"");
for(int i=0;i<countfor5;i++){
strcat
(str5,"*");
}

cout<<"\n"<<"Output"<<"\n";
cout<<"1
"<<str1<<"\n";
cout<<"2
"<<str2<<"\n";
cout<<"3
"<<str3<<"\n";
cout<<"4
"<<str4<<"\n";
cout<<"5
"<<str5<<"\n";
getch();
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS