Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NEED YOUR HELP

Status
Not open for further replies.

nylonelyguy

Technical User
Sep 9, 2003
7
US
hello all! i am hoping to get some help here. i am new to programming. i was asked to do these 2 programs. im sure they are nothing to u. so please who ever can help reply to my post.
1. write a program that read five numbers between 1 and 30 for each number read, your program should print al ine containing that number of adjacent asterisks. example.
2 4 5 6 8
**
****
*****
******
********
i tried here but that didnt work.
#include <iostream.h>


void main()
{
int i;
cout<< "Enter 5 numbers between 1 and 30 ";
cin >>i;

//--- Loop counting i up from 1 to n.
// Initialize counter.
for ( i=1; i<=30;)
{
// Test counter.
cout << "*";
i++; // Increment counter.
}
cout << endl;


}

2. second program is to write a program to compute the exponent of x^n. im not allowed to use pow() function.
where{x>0 ; n>=0}.
example
s^3=8

i tried this , but i dont think it is right.
#include <iostream.h>

void main(void)
{
int num = 0;

cout << "Number Number Squared Number Cube\n";
cout << "-------------------------------------------\n";
while (num++ < 10)
cout << num << "\t\t\t\t" << (num * num)<<"\t\t\t\t"<<(num*num*num) << endl;

}


Thank you so much
 
1. Your first program:

A. It only reads in one integer. You want it to read in 5 integers.

B. You read that integer into a variable, then you clobber that integer by writing 1 into that variable at the start of the for loop.

C. Your for loop goes through 30 times, no matter what. You want the number of iterations to be dependant on the number input.

D. Some style issues: normally, instead of incrementing i at the end of the loop code, you'll put that as your third expression to "for". Also, it's conventional to start loops at 0.


2. Your second program:

A. You never get the values you want to use in your expression.

B. Your loop doesn't really calculate anything. It just outputs the squares and cubes of the first 10 numbers.

Instead of (num), (num*num), and (num*num*num), try to figure out how to calculate num^n by going through a loop n times (where n is any positive integer).


3. Both programs:

A. Don't use <iostream.h>. The official, Standard header, the one you should be using, is <iostream> (without the .h).

B. The function "main" does not and never has returned void. That usage is simply invalid. Have it return int instead.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top