| View previous topic :: View next topic |
| Author |
Message |
NeverForgotten Grandmaster Cheater Supreme
Reputation: 2
Joined: 20 Oct 2007 Posts: 1616 Location: Wii
|
Posted: Mon Sep 21, 2009 7:16 pm Post subject: First Difficult Coding C++ |
|
|
Our teacher has given us this assignment
Write a program that inputs number from the user and prints out all
the squares from 1 to that number
We have recently finished a guessing game that ask the user for the range input and you guess a number between that range
Now at this assignment
I have a basic Cout command
and i just get stuck from there
_________________
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Mon Sep 21, 2009 7:36 pm Post subject: |
|
|
something like this ?
| Code: | #include <iostream>
using namespace std;
int main()
{
int x;
cout << "enter choice: ";
cin >> x;
for( int i = 1; i*i <= x; i++ )
cout << i*i << "\n";
cin >> x;
return 0;
} |
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Sep 21, 2009 7:49 pm Post subject: |
|
|
I wasn't sure if you meant x number squared or square root
| Code: | #include <iostream>
int main()
{
int butt;
std::cin >> butt;
for(int i = 0; i <= butt; i++)
std::cout << (i * i) << std::endl;
return 0;
} |
| Code: | #include <iostream>
#include <cmath>
int main()
{
int dong;
std::cin >> dong;
for(int i = 0; i < (dong + 1); i++)
std::cout << sqrt((float)i) << std::endl;
return 0;
} |
| Code: | #include <iostream>
#include <cstdlib>
int main()
{
int low, high, r, guess = 0;
std::cin >> low >> high;
r = (rand()%((high - low) + 1) + low);
while(guess != r)
std::cin >> guess;
return 0;
} |
|
|
| Back to top |
|
 |
NeverForgotten Grandmaster Cheater Supreme
Reputation: 2
Joined: 20 Oct 2007 Posts: 1616 Location: Wii
|
Posted: Mon Sep 21, 2009 8:06 pm Post subject: |
|
|
Thanks Slugsmack I'll try that code
Do you know any guides or tutorials that would help me with these kinds of codes?
Well I mean if i put 16, it would display these numbers and possibly square 16 as well
1,2 9,16
_________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Mon Sep 21, 2009 8:17 pm Post subject: |
|
|
I gave you a link in the last thread you posted...
cplusplus.com
I'm not entirely sure what you want? No idea where you got 2 from for 16
| Code: | | for(int i = 0; (i * i) <= butt; i++) |
16 will output
0 1 4 9 16, which is 4 squared.
| Code: | | for(int i = 0; i <= butt; i++) |
16 will output all the way up to 256, which is 16 squared
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Mon Sep 21, 2009 8:18 pm Post subject: |
|
|
| slovach wrote: | I gave you a link in the last thread you posted...
cplusplus.com
I'm not entirely sure what you want? No idea where you got 2 from for 16
| Code: | | for(int i = 0; (i * i) <= butt; i++) |
16 will output
0 1 4 9 16, which is 4 squared.
| Code: | | for(int i = 0; i <= butt; i++) |
16 will output all the way up to 256, which is 16 squared |
He wanted from 1 to 'that number'
|
|
| Back to top |
|
 |
talkerzero Grandmaster Cheater
Reputation: 1
Joined: 24 Jul 2008 Posts: 560 Location: California
|
Posted: Mon Sep 21, 2009 8:40 pm Post subject: Re: First Difficult Coding C++ |
|
|
Slugsnack,
| NeverForgotten wrote: | | the squares from 1 to that number |
A square is the product of a number multiplied by itself.
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Mon Sep 21, 2009 8:45 pm Post subject: Re: First Difficult Coding C++ |
|
|
| talkerzero wrote: | Slugsnack,
| NeverForgotten wrote: | | the squares from 1 to that number |
A square is the product of a number multiplied by itself. |
yes, i meant the squares. it's clear i understood the question from the first code snippet i provided. i was letting slovach know 0 should not be one of the elements shown
|
|
| Back to top |
|
 |
NeverForgotten Grandmaster Cheater Supreme
Reputation: 2
Joined: 20 Oct 2007 Posts: 1616 Location: Wii
|
Posted: Mon Sep 21, 2009 9:35 pm Post subject: |
|
|
I can't seem to find anything on cplusplus.com that deals with these kinds of aspects
Also he just wants the user to input a number and I output numbers that square from that number from 1
_________________
|
|
| Back to top |
|
 |
igoticecream Grandmaster Cheater Supreme
Reputation: 0
Joined: 23 Apr 2006 Posts: 1807 Location: 0x00400000
|
Posted: Tue Sep 22, 2009 6:54 pm Post subject: |
|
|
Here is another solution, i love the code that use recursion
| Code: |
void Calc(__in int TopNumber, __in int LowNumber)
{
if((LowNumber) < (TopNumber))
Calc(TopNumber,LowNumber+1);
printf("Square of %d is: %d\n",LowNumber,(LowNumber*LowNumber));
}
int _tmain(int argc, _TCHAR* argv[])
{
int TopNumber;
printf_s("Input a number: ");
scanf_s("%d",&TopNumber);
Calc(TopNumber,1);
return 0;
}
|
|
|
| Back to top |
|
 |
NeverForgotten Grandmaster Cheater Supreme
Reputation: 2
Joined: 20 Oct 2007 Posts: 1616 Location: Wii
|
Posted: Tue Sep 22, 2009 8:35 pm Post subject: |
|
|
| igoticecream wrote: | Here is another solution, i love the code that use recursion
| Code: |
void Calc(__in int TopNumber, __in int LowNumber)
{
if((LowNumber) < (TopNumber))
Calc(TopNumber,LowNumber+1);
printf("Square of %d is: %d\n",LowNumber,(LowNumber*LowNumber));
}
int _tmain(int argc, _TCHAR* argv[])
{
int TopNumber;
printf_s("Input a number: ");
scanf_s("%d",&TopNumber);
Calc(TopNumber,1);
return 0;
}
|
|
Thanks but it would be great if you could use a more simpler code
as we dont use printf, calc, etc
its more cout, and + etc
_________________
|
|
| Back to top |
|
 |
|