0

Hello All...

To be honest, this is a bit overwhelming, but I'm hoping that someone might be able to steer me in the right direction. I am currently taking a correspondence course in Computer Programming, and without instructional support, am finding that the material can be somewhat overwhelming.

I'm looking to find someone who has taken a similar course, preferably from Stratford Career Institute, and who might be able to assist me with some questions I have.

Thanks all for reading my post.

Terry

4
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by Adak
Featured Replies
1

You are more likely to get your questions answered if you actually post them.

0

The problem I have been given is to write a program that calculates the squares and the cubes of the numbers from 0 to 10 and uses tabs to print out the table. The part where I'm getting hung up is that I am only allowed to use the if statement.

I wrote the following code to show me what the output should be, however the keyword in the question is to "write a program that CALCULATES..."

/* Calculate the squares and cubes of the numbers from 0 to 10 */
/* using tabs to separate the columns */

#include "stdafx.h"
#include <stdio.h>

int main ( void )
{
/* Not proper code - Simply shows what output should look like  */

	printf( "number\tsquare\tcube\n" );
	printf( "0\t0\t0\n" );
	printf( "1\t1\t1\n" );
	printf( "2\t4\t8\n" );
	printf( "3\t9\t27\n" );
	printf( "4\t16\t64\n" );
	printf( "5\t25\t125\n" );
	printf( "6\t36\t216\n" );
	printf( "7\t49\t343\n" );
	printf( "8\t64\t512\n" );
	printf( "9\t81\t729\n" );
	printf( "10\t100\t1000\n\n" );
	
	return 0;
}

Edited by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

0

I've double checked the statements I was introduced to in this particular chapter, and basically it's the if statement that was being emphasized. I have checked all of the coding examples in the chapter, and the for statement was not introduced.

I do appreciate your help with this problem.

0

I state this because, here's the code using a for statement

#include <stdio.h>

#define TABLE_SIZE 10

int main(int argc, char**argv)
{
	unsigned int i = 0;
	fputs("num#\tsqr#\tcude#\n", stdout);

	for (i = 0; i <= TABLE_SIZE; ++i)
		fprintf(stdout, "%u\t%u\t%u\n", i, i * i, i * i * i);
	return 0;
}

I really can't envision a neat solution with an if statement...

Edited by gerard4143: n/a

1

Maybe the idea was to simply explicitly print the answers, and getting the data columns, lined up.

There is no choice needed, so no if statement is necessary, imo.

Standard practice would be to use a loop to print this table up.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.