Click to See Complete Forum and Search --> : "C" programming help (Newbie)


thephreak6
10-19-2002, 05:24 PM
Im taking a C programming course in my crappy community college and one of the projects is to:

Write a C function named PowerTable() that will create a table of squares and cubes. The function should accept as parameters the starting number for the table, the number of rows in the table and the increment (always an integer) for each row of the table

The function will output to the screen the following table:

Number Square Cube
-----------------------------
1.5000 2.2500 3.3750
3.5000 12.2500 42.8750
5.5000 30.2500 166.3750

My problem is I can only get the "number" colum to work,
the square and cube all come out as 0.00000.
Ive spent hours trying to figure out what I did wrong, and I can't see it. Any suggestions?

Thanks everyone, you all rock :D

#include <stdio.h>

double start;
int rows;
double incr;



/* double for starting number, integer for rows (can't have half a row), double for incriment value
(can incrimint by 1.5 and etc) */


int PowerTable(double, int, double);

void main(void)
{

rows = 0;

printf("How many rows do you wish to have?\n");
scanf("%d", &rows);

printf("What do you wish the starting number for the table to be?\n");
scanf("%d", &start);

printf("How much do you wish each number to be incriminted?\n");
scanf("%d", &incr);


PowerTable(start, rows, incr);

}

PowerTable(double fstart, int frows, double fincr )
{
int timesdone;
double square[500];
double cube[500];
timesdone = 0;

printf("\tNumber \t\t\t Square\t\t\t Cube\n");
printf("\t--------------------------------------------------\n");

while(timesdone <= frows)
{

square[timesdone] = (fstart * fstart);
cube[timesdone] = fstart * fstart * fstart;

printf("\t%d \t\t\t \t %5.2f \t\t\t %5.2f\n", fstart, square[timesdone], cube[timesdone]);

fstart += fincr;
timesdone++;

}

printf("Done\n");



return(0);
}

yolabingo
10-19-2002, 05:40 PM
Good question. I don't immediately see the problem. One point I'd mention is that you probably want your main program to be an int. Eg:
int main()

instead of void main(void)
especially because you end with "return 0" -- a void function cannot return anything. (My version of gcc wouldn't even compile it until I changed this...)

askrieger
10-19-2002, 05:41 PM
In main() your trying to read in what you want to be fooating point numbers with a %d format. but %d gives integers.

TacKat
10-19-2002, 05:50 PM
It's not immediately apparent to me why you're having problems, but I do see some other potential problems.

You don't need to use global variables. The whole point of passing variables into the PowerTable function is so that you can avoid globals.

You don't need to make arrays to hold the squares and cubes. Since you don't use them after you print them, you can just make the calculations in a temporary variable (or in the printf itself). This way you don't even have to keep track of how many times you've done the loop; you just do it.

truls
10-20-2002, 06:28 AM
Since I had a compiler I tried your program and solved the problem.

Your problem is that double is written as "lf" ( small L + f ) on input and output. Also you have mixed up your doubles and integers in your output statement.

To input a double you write:
scanf( "%lf", &double_var);
And similarly when you output a double.

This is the only flaw in your program though, the rest works wonders. All this info is in "man scanf" by the way :D