Been stumped for a good day now on this one, I know I just need the "=" sign and ">" end piece to the question.Any hints or advice>?
int main()
{
//Calling in Variables
int data;
int counter;
//Step 1:Prompt the user to pass in a positive integer between (0-50)
cout << "Type in a number between 0-50 to plot your bar: " << endl;
cin >> data;
//Step 2: Error check for bad numbers that have been input.
if(data < 0) {
cout<< "ERROR: " << data << " is not in acceptable range."<< endl;
}
//Step 3:
if(data >= 0 && data <= 50){
for(int counter =1; counter <= data; counter++) {
if(counter % 10 !=0)
cout<< "|";
else(data%5 == 0)
cout << "+";
}
}
else
cout << "BarPlot - End Plot by User Request"
return (0);
}
Create a simple Bar Graph Plotter (BarPlot) that takes in a positive integer number between [0 50], and plots the
number as a simple bar. The program should only accept data within this range. Any negative value input should
terminate the Program. Any input larger than 50 should result in an error message and a prompt to enter another
number.
Example Output:
BarPlot – A Simple Bar Graph Plotter:
Input a number in range [0 50] or a negative number to terminate:
| Enter Number: 8
| ====+==> 8
| Enter Number: 15
| ====+====|====> 15
| Enter Number: 50
| ====+====|====+====|====+====|====+====|====+====> 50
| Enter Number: 65
| ERROR: 65 is not in acceptable range.
| Enter Number: 0
| 0
| Enter Number: 1
| > 1
| Enter Number: 5
| ====> 50
| Enter Number: -1
BarPlot – End Plot by User Request
You have a really simple question that you have made complex by putting it in a big lump of text. You have pretty simple logic, you want to print out = except for every 5th and 10th number. You were basiclly already there:
for(int counter =1; counter <= data; counter++) {
if(counter % 10 ==0)
cout<< "|";
else if(counter%5 == 0)
cout << "+";
else
cout << "=";
}
cout << ">" << yournumberthing << endl;
You just needed to think about it some more.
Related
I've met some problem during programming. I want to write a program to differentiate even numbers, odd numbers, zero values and negative numbers by using while and for loop.
1st question :
However, when I try to run my program, the last number I've entered will not be counted. I know it occur because of my o++ put at the top of the if condition, how should I solve my problem?
2nd question :
For the for loop parts, actually it may ignored those negative values. How should I solve it to let the negative numbers also count in loop ? May I changed the num>0 to num < 100000 to let the for loop works?
#include<iostream>
using namespace std;
#include<iostream>
using namespace std;
int main ()
{
int num ,numbers = 1 ;
char answer = 'Y' ;
int o=0, e=0, z=0 ,n=0 ;
// o for odd numbers, e for even numbers, z for zero values, n for negative numbers
cout << "Enter number" << numbers << ": " << endl ;
cin >> num ;
for ( num = num ; num >0; num++)
while (answer == 'y' || answer == 'Y')
{
if (num % 2 == 0 && num > 0)
{
e++ ;
cout<< "The number of even numbers is :" << e << endl;
numbers ++ ;
cout<<"Please enter number" << numbers << endl ;
cin >> num ;
cout<<"If you wish to continue, Please enter y or Y to continue this program : "<< endl ;
cin>> answer ;
}
else if (num % 2 == 1 && num > 0)
{
o++;
cout<< "The number of odd numbers is :" << o << endl;
numbers ++ ;
cout<<"Please enter number" << numbers << endl ;
cin >> num;
cout<<"If you wish to continue, Please enter y or Y to continue this program : "<< endl ;
cin>> answer ;
}
else if (num == 0)
{
z ++;
cout<< "The total of 0 is :" << z << endl;
numbers ++ ;
cout<<"Please enter number" << numbers << endl ;
cin >> num;
cout<<"If you wish to continue, Please enter y or Y to continue this program : "<< endl ;
cin>> answer ;
}
}
cout << "The total even numbers is :" << e << endl;
cout << "The total odd numbers is :" << o << endl ;
cout << "The total negative numbers is :" << n << endl ;
cout << "The total zero number is:" << z << endl;
return 0;
}
This line, in main() is really puzzling:
// ...
for ( num = num ; num >0; num++)
while (answer == 'y' || answer == 'Y')
The for(;;) statement is your main loop. The while statement will be executed as long as num is positive.
Let's look at this for() statement in detail:
for (num = num; // num = num ??? this statement does nothing.
num > 0; // the while statement (and the contents of the whule() loop block)
// will only execute if num is > 0.
++num) // if num was > 0 then this loop will run until num overflows...
Removing the for(;;) statement will make your program run a lot better.
Your o++ has nothing do with it.
(Perhaps you have been so convinced about that being the problem that you didn't think of looking elsewhere. It happens to everyone.)
The problem is that your sequence is this:
Check the most recently entered number and print the result
Ask the user for a number, but don't do anything with it
Ask the user whether they want to continue
If they want to continue, repeat from item 1
If they don't, stop counting
And since you stop counting if the user doesn't want to continue, the last number seems to have disappeared.
Fixing it left as an exercise.
(Think more carefully about which order you need to do things in.)
Handling negative numbers requires you to write some code to do that - you handle two cases of positive numbers, and one for zero, but you must have forgotten about the negatives.
Fixing this also left as an exercise.
As the title suggests, my program which is designed to find the largest element within a user defined integer array becomes unstable when all negative numbers are used for the user input. Additionally the output becomes unstable when all zero's are used for the user input (excluding the input for total # of elements).
The program works just fine when all positive numbers are used. This is quite perplexing to me and I'm sure there's a valid explanation but I was under the impression that in C++, the int & float data types were automatically signed and would be able to handle negative numbers for it's data range. So why would the program not return a valid output if all negative numbers are used for the user array input(s)?
Bad Output:
Please enter a total number of elements you'll be using: 5
Please enter each variable one by one.
Enter number 1: -10
Enter number 2: -5
Enter number 3: -20
Enter number 4: -2
Enter number 5: -7
The largest element within specified realNum[5] array is element number: 6 with a value of: 5.88501e-039
Good Output:
Please enter a total number of elements you'll be using: 5
Please enter each variable one by one.
Enter number 1: 10
Enter number 2: 5
Enter number 3: 20
Enter number 4: 2
Enter number 5: 7
The largest element within specififed realNum[5] array is element number: 3 with a value of: 20
Program:
//10.2 Largest Element Finder of an Array
//Mandatory header
#include <iostream>
//Use namespace std ;
using namespace std ;
//Mandatory main method
int main ()
{
//Declare and initlize variables
int i, total, realNum = 0, temp = 1 ;
//Ask user to input a number of total elements
cout << endl << endl
<< "Please enter a total number of elements you'll be using: " ;
//Wait for user input
cin >> total ;
//Declare array set
float setNum [total] ;
//Ask user to input each varaible
cout << endl
<< "Please enter each variable one by one." << endl << endl ;
for ( i = 0 ; i < total ; i ++ )
{
cout << endl << "Enter number " << (i + 1) << ": " ;
cin >> setNum [i] ;
}
//Find the largest element within the array
for ( i = 0 ; i < total ; i ++ )
{
if ( setNum [i] <= setNum [temp] ) //Discard current i if less than the next element - Means temp is HIGHER and should be saved
{
if ( setNum [temp] >= setNum[realNum] )
realNum = temp ; //Temp can now be changed for iteration purposes as realNum is saving the highest element's positon
}
else if ( setNum [i] >= setNum [temp] ) //Discard current i if more than the next element and use the remainder to compete against the realNum
{
if ( setNum [i] >= setNum [realNum] )
realNum = i ;
}
i ++ ;
temp += 2 ;
}
//Display calculations
cout << endl << endl
<< "The largest element within specififed realNum[" << total << "] array is element number: " << (realNum + 1) << " with a value of: " << setNum[realNum] << endl ;
//Mandatory return statement
return 0 ;
}
When I enter a value between 0 to 70 it prints infinitely (which is wrong), but when input a number that is not between 0 to 70 it prompts the user to enter a number that should be between 0 to 70. (which is correct)
while (true)
{
if (hourswork < 0 || hourswork > 70)
{
cout<<"Please enter a value between 0 to 70: ";
cin>>hourswork;
}
else
{
wages = RATE * hourswork;
cout << "The wage is: " << wages << endl;
}
}
The result when I enter a value between 0 to 70 should only be printed once.
You should reconsider what you have in your while loop. Do you really want the output in there? You probably only want the loop to get input until it's valid. The output should instead be moved out of the loop:
int main() {
int hourswork = -1;
int RATE = 123;
while (hourswork < 0 || hourswork > 70)
{
cout << "Please enter a value between 0 to 70: ";
cin >> hourswork;
}
int wages = RATE * hourswork;
cout << "The wage is: " << wages << endl;
}
This way it will ask for a hourswork value until it is valid, which then stops the loop, and unconditionally does the print part once after the loop.
Generally the reason why your loop never terminated is because while (true) won't stop looping unless you get out of it with a break, return or something similar, neither of which is present in your code.
You need to leave your loop with a break when your hourswork was within range.
Like this:
while (true)
{
if (hourswork < 0 || hourswork > 70)
{
cout<<"Please enter a value between 0 to 70: ";
cin>>hourswork;
}
else
{
wages = RATE * hourswork;
cout << "The wage is: " << wages << endl;
break;
}
}
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int number=0;
cout<<"enter an number to cumpute";
cin>>number;
if(number=0)
cout<<"0"<<endl;
for(number>0;51>number;) {
number--;
cout<<"=";
}
for(number>10;number%10==0;) {
cout<<"|";
}
for(number>5;number%5==0;) {
cout<<"+";
}
cout<<endl;
system("PAUSE");
return 0;
}
(i got textbook called by Y.Daniel Liang. I can not find any thing like this) I have no idea how to make this loop work and I try use "while" and not working either. Should i just cout the "=" "+" "|", or start as string. I hope the output look like this.
BarPlot – A Simple Bar Graph Plotter:
Input a number in range [0 50] or a negative number to terminate:
| Enter Number: 6
| ====+> 6
| Enter Number: 12
| ====+====|=> 12
| Enter Number: 50
| ====+====|====+====|====+====|====+====|====+====> 50
| Enter Number: 53
| ERROR: 53 is not in acceptable range.
| Enter Number: 33
| ====+====|====+====|====+====|==> 33
| Enter Number: 0
| 0
| Enter Number: 5
| ====> 5
| Enter Number: -1
------------------------------------------------
BarPlot – End Plot by User Request
There is no need for you to convert a number to a string in order to solve the problem. The object cout can handle printing both numbers and strings without you needing to cast between them.
//example
int number = 1;
string str = "hello;
char c = '!';
//print hello1!
cout << str << number << c;
Here is a solution to the problem that does not require the need to cast an integer to a string.
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
//output inital prompt
cout << "BarPlot – A Simple Bar Graph Plotter:\n";
cout << "Input a number in range [0 50] or a negative number to terminate.\n\n";
//read in input
int number = 0;
cout << "Enter Number: ";
cin >> number;
//continue asking for input until a negative number is given
while (number >= 0){
for (int i = 1; i < number; ++i){
//special symbol every 10th char
if (i % 10 == 0) cout << "|";
//special symbol every 5th char
else if (i % 5 == 0) cout << "+";
//every other char
else cout << "=";
}
//print 0 or the number with an arrow before it
if (number == 0) cout << 0;
else cout << "> " << number << "\n";
//re-ask for input
cout << "\nEnter Number: ";
cin >> number;
}
//output ending message
cout << "BarPlot – End Plot by User Request\n";
return EXIT_SUCCESS;
}
Here is my new edited code I am having trouble with my final if loop for the X and O's stored inside an array. I am trying to check if there is an X or O already inside the array or not. If there is one it asks the user for another input then start over again however when i run the program this happens:
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Please Enter Box Number: 1
player X your number is 1
X | 2 | 3
4 | 5 | 6
7 | 8 | 9
Please Enter Box Number: 1
Please enter another Number2
player O your number is 1
O | 2 | 3
4 | 5 | 6
7 | 8 | 9
Please Enter Box Number:
char c;
cout << "Please Enter Box Number: ";
cin >> c;
if (c > '9' || c < '0')
{
// error message
cout << "please enter a number 1-9" << " ";
cin >> c;
cin.ignore(numeric_limits<int>::max(), '\n');
}
if (c < '9' || c > '0')
{
int number = c - '0';
if (board[number - 1] == 'X' || board[number - 1] == 'O')
{
cout << "Please enter another Number";
cin >> c;
cin.ignore(numeric_limits<int>::max(), '\n');
}
else if (board[number -1] != 'X' || board[number - 1] != 'O' ) {
if (player == 'X')
{
player = 'O';
}
else
{
player = 'X';
}
cout << "player " << player << " your number is " << number << endl;
// Your implementation here...
board[number - 1] = player;
}
}
}
First the second question (the easy one):
Return exits from the current function. So, after the return, no further instruction in your function is executed. You should remove the return and put the rest of the code (that should not be executed if the input is not valid) in the else block of that if.
First question:
Why do you check that the index of each position in the array (as in board[7] = '8'). You already know the position, what you need to mark in each cell is the player who uses it; for example set values of 'X' (player 1), 'O' (player 2), ' ' (empty).
Instead of nine "if", it will be easier if you convert your input 'c' into an integer and use it as an index. With ASCII characters (I do not know if this still workds with WCHAR, UTF-8 and so on) you could make it quick and dirt:
int k = c - '0';
board[k] = player;
Edit:
Taking into account checking that the square is not already used:
int k = c - '0';
if (board[k] == ' ') {
board[k] = player;
} else {
cout << "This square is already used";
}
To loop back until a valid value has been entered, you can do something like this:
int number = 10;
while( ( number > 9 ) || ( number < 0 ) )
{
std::cout << "Please Enter Box Number: ";
std::cin >> number;
}
std::cout << "Your number is " << number << std::endl;
I am not too sure what you are asking about the other part. I will edit my response if/when I do.
http://www.asciitable.com/
if you look at the ascii chart, you will notice that all of the characters have a decimal value to them. So when your teacher subtracts '0' from what the user entered, say '1' he is really doing:
int number = (int)'1' - (int)'0'; // x will equal 1
your array can then be accessed directly using [ ] notation so:
board[number] = player