I am trying to successfully complete this problem for my c++ class. Any help or comments would be greatly appreciated!
In this exercise, you create a program that displays the numbers 0-117, in increments of nine. Write two versions of the appropriate code one using the while statement and the other using the for statement. The numbers 0,9,18 and so on should appear twice on the the screen.
This is what I have for the while statement.
//Labintroductory-25.cpp - dislplays numbers in sets of nine
//Created/revised by Kirstin Griffin on April 7, 2013
#include <iostream>
using namespace std;
int main ()
{
//declare variables
int numbers =0;
int setsOfNumbers =0; //accumulator
//display first set of numbers
cout << "Numbers:";
cin >> numbers;
cout <<"Set 1:";
while (numbers == 0-117)
{
//update accumulator, then get another set of numbers
setsOfNumbers == numbers / 9;
cout << "Set 1:";
cin >> numbers;
} //end while
if (numbers = 0 - 9)
setsOfNumbers = 1;
else if (numbers = 9 - 18)
setsOfNumbers = 2;
else if (numbers = 18-27)
setsOfNumbers = 3;
else if (numbers = 27-36)
setsOfNumbers = 4;
else if (numbers = 36-45)
setsOfNumbers = 5;
else if (numbers = 45-54)
setsOfNumbers = 6;
else if (numbers = 54-63)
setsOfNumbers = 7;
else if (numbers = 63-72)
setsOfNumbers = 8;
else if (numbers = 72-81)
setsOfNumbers = 9;
else if (numbers = 81-90)
setsOfNumbers = 10;
else if (numbers = 90-99)
setsOfNumbers = 11;
else if (numbers = 99-108)
setsOfNumbers = 12;
else if (numbers = 108-117)
setsOfNumbers = 13;
//end if
//display the set of numbers
cout << "Sets of numbers:" << setsOfNumbers << endl;
system("pause");
return 0;
}//end of main function
Your conditions are really obscure. F.e. (numbers = 0 - 9) will ever evaluate to true. You're assigning the value -9 to variable numbers. C++ takes any numerical expression as boolean. An expression evaluating to 0 is taken as false while any other one will be interpreted as true.
setsOfNumbers == numbers / 9; The assignment operator in C++ is = while comparing something for equality is done using ==.
Well my question really is how can I get this to display the numbers like I need them to be displayed? I am new to this programming class and I am having a difficult time picking all of this up. So what do you suggest I change?
I think I fixed the if statement but I'm still having problems with displaying the actual numbers in the first place. Is my while statement on the right track?
//Labintroductory-25.cpp - dislplays numbers in sets of nine
//Created/revised by Kirstin Griffin on April 7, 2013
#include <iostream>
using namespace std;
int main ()
{
//declare variables
int numbers =0;
int setsOfNumbers =0; //accumulator
//display first set of numbers
cout << "Numbers:";
cin >> numbers;
//display numbers 0 to 117
cout <<"Set 1:";
while (numbers == 0-117)
{
//update accumulator, then get another set of numbers
setsOfNumbers == numbers / 9;
cout << "Set 1:";
cin >> numbers;
Your while statement. You need to change it to the same format as your condition in your first if statement if you meant for numbers to be in between 0 and 117, i.e. :
numbers >= 0 && numbers <= 117
Oh, and you ignored tcs's advice on your "setsOfNumbers" assignment line. Use a single "=" sign, not two.
On a side note, to make your code easier to read, there is a handy-dandy "code" tag you can use. It's the "<>" button to the right.
In this exercise, you create a program that displays the numbers 0-117, in increments of nine. Write two versions of the appropriate code one using the while statement and the other using the for statement. The numbers 0,9,18 and so on should appear twice on the the screen.
Sorry, but your code has nothing to do with the given exercise:
- What do you want to read in from cin?
- What do you want to write onto cout?
- What's the meaning of your integer variable setsOfNumbers?
I changed those few corrections. Tcs, I honeslty don't know what else to do. Im not familiar with all of the information you included in the questions. I'm really just trying to figure out the right cin's and cout's to use to display the numbers 0-117 in sets of nine. I was trying to use the variable name setsOfNumbers as the output? Is that not right?
//Labintroductory-25.cpp - dislplays numbers in sets of nine
//Created/revised by Kirstin Griffin on April 7, 2013
#include <iostream>
using namespace std;
int main ()
{
//declare variables
int numbers =0;
int setsOfNumbers =0; //accumulator
//display first set of numbers
cout << "Numbers:";
cin >> numbers;
//display numbers 0 to 117
cout <<"Set 1:";
while (numbers >= 0 && numbers <= 117)
{
//update accumulator, then get another set of numbers
setsOfNumbers = numbers / 9;
cout << "Set 1:";
cin >> numbers;
that displays the numbers 0-117, in increments of nine.
1. Your program takes any number from user until it is less than 0 or greater than 117.
2. It displays the result of numbers / 9. F.e. if the users enters 200 the program outputs the value 22 (which is the integral part of 200 / 9).
But as I understand your exercise it should output
0, 9, 18, 27, ...
You should read the tutorial http://www.cplusplus.com/doc/tutorial/. There have a special look at "Control Structures" and "Basic Input/Output". You may also want to read chapter "Variables. Data Types" to understand the concept of the data type int.
Your right about the output, but I have no idea how to make that happen. I read the tutorials and I have thought about your questions, but I am just beginning this course and I don't know all of the definitions and information. I just want to make the program run sucessfully to show the numbers 0-117 in sets of nine.
In this exercise, you create a program that displays the numbers 0-117, in increments of nine. Write two versions of the appropriate code one using the while statement and the other using the for statement. The numbers 0,9,18 and so on should appear twice on the the screen.
There's nothing to be read from cin.
So fix it by removing everything having to do with reading anything from cin.
And finally: Really spend some time to read the tutorial and try its examples! Maybe you'll come back in a few days after getting some answers from the tutorial.
Holy crap the advice you received on this thread is terrible.
In this exercise, you create a program that displays the numbers 0-117, in increments of nine. Write two versions of the appropriate code one using the while statement and the other using the for statement. The numbers 0,9,18 and so on should appear twice on the the screen.
So your professor wants 0, 9, 18, 27 etc. Because the advice you received so far has been unhelpful here are the 2 loops you need, just put them in to your main method and change variable names as required.
How to do it in 2 ways.
1. For Loop
1 2 3 4 5 6 7 8
for (unsigned i = 0; i <= 117; ++i) {
if (i % 9 == 0)
cout << i << endl;
}
// or
for (unsigned i = 0; i <= 117; i += 9)
cout << i << endl;
2. While loop
1 2 3 4 5
unsigned i = 0;
while (i <= 117) {
cout << i << endl;
i = i + 9; // also can be written as i += 9
}
Oh my gosh Zaita you are a life saver!! I have been struggling with this for three days! My professor is really strict though, do you think you can help me clean it up?
//Labintroductory-25.cpp - dislplays numbers in sets of nine
//Created/revised by Kirstin Griffin on April 7, 2013
#include <iostream>
using namespace std;
int main ()
{
//declare variables
int numbers =0;
int sets =0;
//display first set of numbers
cout << "Numbers (-1 to stop):";
cout << 0-117;
#include <iostream>
usingnamespace std;
int main()
{
//using for loop
for (unsigned i = 0; i <= 117; i += 9)
{
cout << i << endl;
}
//using while loop
unsigned i = 0;
while (i <= 117)
{
cout << i << endl;
i += 9;
}
return 0;
}