someone please help

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: May 2007
Posts: 30
Reputation: nkhosinathie is an unknown quantity at this point 
Solved Threads: 0
nkhosinathie nkhosinathie is offline Offline
Light Poster

someone please help

 
0
  #1
May 10th, 2007
i'm developing a c++ program here and also i'm nwe to c++.
i have been given a program that has to print integers from 0-10 with their squares and cubes in a table form. but the problem is i'm only allowed to use if statement onyl to do this program.well this is how i did it.

#include<iostream>
using namespace std;
using std::cout;
using std::cin;
using std::endl;

int main()
{
int a;int b;int c;int d;int e;int f;int g;int h;int i;int j;int k;
int square;
int cube;

cout<<"enter any number"<<endl;
cin>>a;
square=a*a;
cube=a*a*a;

cout<<a<<"\t"<<square<<"\t"<<cube<<endl;

cin>>b;
square=b*b;
cube= b*b*b;

cout<<b<<"\t"<<square<<"\t "<<cube<<endl;

return 0;
}

i repeated the statement until i used all the variables above.
it appears like this

integer square cube
0 0 0
1
1 1 1
2
2 2 2

my problem is how to remove this numbers between because it has to appear like this

integer square cube
0 0 0
1 1 1
2 2 2
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,804
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: someone please help

 
0
  #2
May 10th, 2007
I imagine the problem was designed to force you to use a recursive solution. That's really the only way to loop without using a loop.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 30
Reputation: nkhosinathie is an unknown quantity at this point 
Solved Threads: 0
nkhosinathie nkhosinathie is offline Offline
Light Poster

Re: someone please help

 
0
  #3
May 10th, 2007
so how can i use the recursive loop because firstly i'm new to c++ programming and also this programming has to be done using if statement only.i'm prepared to listen and learn.i'm not here to copy any stqatements i'm here tyo learn
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 114
Reputation: kylcrow is an unknown quantity at this point 
Solved Threads: 2
kylcrow kylcrow is offline Offline
Junior Poster

Re: someone please help

 
0
  #4
May 10th, 2007
instead of writing
using std::cout;
using std::cin;
using std::endl;

you can just write
using namspace std;
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 30
Reputation: nkhosinathie is an unknown quantity at this point 
Solved Threads: 0
nkhosinathie nkhosinathie is offline Offline
Light Poster

Re: someone please help

 
0
  #5
May 10th, 2007
thanks i will do. but still whenever i compile this program it is leaving this numbers in between so i don't know how to deal with that.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 30
Reputation: nkhosinathie is an unknown quantity at this point 
Solved Threads: 0
nkhosinathie nkhosinathie is offline Offline
Light Poster

Re: someone please help

 
0
  #6
May 10th, 2007
Originally Posted by kylcrow View Post
instead of writing
using std::cout;
using std::cin;
using std::endl;

you can just write
using namspace std;
thanks i will do. but still whenever i compile this program it is leaving this numbers in between so i don't know how to deal with that.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,804
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: someone please help

 
0
  #7
May 10th, 2007
>i'm prepared to listen and learn.i'm not here to copy any stqatements i'm here tyo learn


>and also this programming has to be done using if statement only
That's exactly why I think you're meant to use recursion. The only other way is to write the code inline:
  1. int main()
  2. {
  3. // Print the square and cube of 0
  4. // Print the square and cube of 1
  5. // Print the square and cube of 2
  6. ...
  7. // Print the square and cube of 9
  8. }
That doesn't use an if statement, and neither does your current solution. So my conclusion is that you need to approximate this:
  1. int main()
  2. {
  3. for ( int i = 0; i < 10; i++ ) {
  4. // Print the square and cube of i
  5. }
  6. }
Which can be done with a recursive function using an if statement as the base case:
  1. void f ( int i )
  2. {
  3. if ( i < 10 ) {
  4. // Print the square and cube of i
  5. f ( i + 1 );
  6. }
  7. }
  8.  
  9. int main()
  10. {
  11. f ( 0 );
  12. }
>you can just write
>using namspace std;
That's generally considered a bad practice though because it opens up names that you don't use and defeats the purpose of namespaces in avoiding naming collisions.

>but still whenever i compile this program it is leaving this numbers in
>between so i don't know how to deal with that.
The numbers in between are the user input being echoed to the screen. You can avoid it by requiring the user to enter all numbers immediately and then simply not prompting further:
  1. int main()
  2. {
  3. int x, y, z;
  4.  
  5. cout<<"Enter three numbers: ";
  6. cin>> x;
  7. cout<< x <<'\n';
  8. cin>> y;
  9. cout<< y <<'\n';
  10. cin>> z;
  11. cout<< z <<'\n';
  12. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 30
Reputation: nkhosinathie is an unknown quantity at this point 
Solved Threads: 0
nkhosinathie nkhosinathie is offline Offline
Light Poster

Re: someone please help

 
0
  #8
May 10th, 2007
Originally Posted by Narue View Post
>i'm prepared to listen and learn.i'm not here to copy any stqatements i'm here tyo learn


>and also this programming has to be done using if statement only
That's exactly why I think you're meant to use recursion. The only other way is to write the code inline:
  1. int main()
  2. {
  3. // Print the square and cube of 0
  4. // Print the square and cube of 1
  5. // Print the square and cube of 2
  6. ...
  7. // Print the square and cube of 9
  8. }
That doesn't use an if statement, and neither does your current solution. So my conclusion is that you need to approximate this:
  1. int main()
  2. {
  3. for ( int i = 0; i < 10; i++ ) {
  4. // Print the square and cube of i
  5. }
  6. }
Which can be done with a recursive function using an if statement as the base case:
  1. void f ( int i )
  2. {
  3. if ( i < 10 ) {
  4. // Print the square and cube of i
  5. f ( i + 1 );
  6. }
  7. }
  8.  
  9. int main()
  10. {
  11. f ( 0 );
  12. }
>you can just write
>using namspace std;
That's generally considered a bad practice though because it opens up names that you don't use and defeats the purpose of namespaces in avoiding naming collisions.

>but still whenever i compile this program it is leaving this numbers in
>between so i don't know how to deal with that.
The numbers in between are the user input being echoed to the screen. You can avoid it by requiring the user to enter all numbers immediately and then simply not prompting further:
  1. int main()
  2. {
  3. int x, y, z;
  4.  
  5. cout<<"Enter three numbers: ";
  6. cin>> x;
  7. cout<< x <<'\n';
  8. cin>> y;
  9. cout<< y <<'\n';
  10. cin>> z;
  11. cout<< z <<'\n';
  12. }
thanks so much for your help i will try it and i will definitely get back to u'thanks
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 30
Reputation: nkhosinathie is an unknown quantity at this point 
Solved Threads: 0
nkhosinathie nkhosinathie is offline Offline
Light Poster

Re: someone please help

 
0
  #9
May 10th, 2007
Originally Posted by nkhosinathie View Post
thanks so much for your help i will try it and i will definitely get back to u'thanks
another thing i can ask if u don't mind, i'm also doing a bank account program but i will do it and come back to u,but what i want to know about it is the understanding of how i have to do.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,804
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: someone please help

 
0
  #10
May 10th, 2007
>but what i want to know about it is the understanding of how i have to do.
That's a different problem and a different question, so create a new thread when you start working on it.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC