Register to reply

C, compiler - write a table from a program

by UNG
Tags: compiler, program, table, write
Share this thread:
UNG
#1
Oct2-07, 12:12 PM
P: 43
Hi,
I am trying to write a program on a C compiler and Im not too sure how to do it.

The question asks to write a full program to calculate the squares and cubes of the numbers from 0 to 10, which uses tabs (\t) to illustrate the data in the table.

Click on the uploaded document to view the question.

"Ques Example.jpg"

Also I have given an example of a square root I have calculated for a number in
document,

"Example of.jpg"

However Im not too sure which instructions are needed to show the information in a table.

Please help, thanks very much !
Attached Thumbnails
Ques Example.jpg   Example of.jpg  
Phys.Org News Partner Science news on Phys.org
World's largest solar boat on Greek prehistoric mission
Google searches hold key to future market crashes
Mineral magic? Common mineral capable of making and breaking bonds
D H
#2
Oct2-07, 01:05 PM
Mentor
P: 15,065
Some questions:
  • What work have you done so far?
  • What is the general layout of your program?
  • Do you know how to do the math, how to print?
  • Do you know how to do this for each integer from 0 to 10?
UNG
#3
Oct2-07, 01:54 PM
P: 43
Well I only know how to add, subtract and multiply groups of numbers together and find the average of them.

I know how to print instructions on the screen, for instance, phrases like "Enter two integers", "Answer/sum ="

Thats about it

D H
#4
Oct2-07, 02:12 PM
Mentor
P: 15,065
C, compiler - write a table from a program

You have to compute the square and cube of a number. You already computed the cube. Print the data using printf, one line at a time. If formatted correctly you will get the desired pretty table. You already used "\n" to generate an end of line. Tab is just another special character: "\t" instead of "\n".

Have you been taught about looping yet?
UNG
#5
Oct2-07, 03:12 PM
P: 43
How do you prevent the numbers from showing up.
Shouldnt it be so that when you type in the number "3" under the number title (for example) all of its possible combinations will show up such as "9" and "27" under the squared and cubed titles.

I think we havent been taught looping yet?
Any example of this?
UNG
#6
Oct2-07, 03:59 PM
P: 43
Hang on,
Do I just type the correct values under each header instead, without it actually being a flowing program.
D H
#7
Oct2-07, 05:09 PM
Mentor
P: 15,065
Looping in 'c' is done with the for or while statement.

I think you are supposed to do the calculations. You could easily write a one statement program that produces the table (just print the table), but that would violate the spirit of the assignment and would probably result in some point deductions.
UNG
#8
Oct2-07, 05:15 PM
P: 43
haha.
Yes thats right.

Um... I have only really mannaged to seperate the three columns. But when I type in the numbers it wont show properly, as Im missing out a large part of the program.
DefaultName
#9
Oct3-07, 10:09 PM
P: 181
Can you post your code? You can post your code on rafb.net/paste - you will be provided with a link - please paste that here.

Also remember to make sure "C" formatting is on when you're on that website.
UNG
#10
Oct8-07, 06:50 PM
P: 43
Yes I have it here now.
It automaticaly calculates the square and cube roots of the first number typed in.

However the first number you type appears in the table as well and will not let me type in more numbers for the next line.
A loop in the program is needed here ????

http://rafb.net/p/CBphkL43.html
pooface
#11
Oct8-07, 09:53 PM
P: 210
yes a loop is needed here.

EDIT sorry I thought you didnt want to input numbers to appear for some reason.

Pay attention to the carriage return that will make your table look bad after scanf.
UNG
#12
Oct10-07, 03:24 PM
P: 43
Okay so what is the carriage return?
Would that be the last part of the program to finish it?

http://rafb.net/p/sCyV4l30.nln.html
NoUse
#13
Oct11-07, 12:31 AM
P: 9
This code is fairly simple as long as your know about the math header and how to use loops. I commented this code fairly well, so you shouldn't have any trouble understanding. Make sure you know what's going on in every line though. This is some VERY BASIC stuff that is extremely vital in computer programming. So it's important for you to learn about this stuff now, rather then later.

#include <stdio.h>
#include <math.h>

int main(void)
{
    /* declare your variables */
    double idx, square, cube;
    
    /* initialize your variables */
    idx = 0;
    square = 0;
    cube = 0;
    
    /* first row of table aka titles */
    printf("number\tsquare\tcube\n");
    printf("------\t------\t-----\t\n");
    
    /* main for loop. prints table */
    for(idx; idx <= 10; idx++){
        /* raise each increment to power of 2 */
        square = pow(idx, 2);
        /* raise each increment to power of 3 */
        cube = pow(idx, 3);
        /* print your floating point variables to zero decimal */
        printf("%.0f\t%.0f\t%.0f\n", idx, square, cube);  
    } 
    
    printf("\nPress Enter to continue...");
    getchar();
    return 0; 
}
UNG
#14
Oct11-07, 07:26 AM
P: 43
Thanks very much for your help!
I have been changing parts in the program to get a better understanding of how it works.

I see that you added in maths header and how the "<= 10" part is there to decide on how many numbers (increasing from 0) are required in the table, as the numbers must be less than or equal to 10. Also the word "double" is an instuction that must be enterd just like "printf, scanf, int" e.t.c for it to be performed.

And then what ever you replace "n" with in "A = pow(x, n);" will determine the power in the coloumn labeld as "A".
The ".0f" after the answer given by "%d" is needed for the answers to appear in that coloumn.
"getchar();" is needed for the black screen to appear on the screen until the user closes it.

But Im not sure why the "++" part is needed after the "<= 10; ix" bit.
???
pooface
#15
Oct11-07, 09:21 AM
P: 210
++ is to increment variable idx so after it deals with one number say 3 in the loop and adds it to the table, it will move to 4 and so on.

idx++ means idx=idx+1. If this was not put then you will get stuck in an infinite loop and have a program crash.
UNG
#16
Oct11-07, 11:41 AM
P: 43
Ah cool, I understand now.

Thanks once again !

Also I tried running the program without the "void" in between the brackets "int main( )" and it seemed to work fine, what is the reason for this?
UNG
#17
Oct11-07, 01:21 PM
P: 43
I have another question and managed to answer it, as it seems like it is working correctly.

Th program below is designed to state whether the integer entered is an Even or Odd number. I just thought there might be a better and more proffesional way of writting it?

Here it is:

http://rafb.net/p/uKmqqQ58.nln.html
NoUse
#18
Oct15-07, 02:06 AM
P: 9
Quote Quote by UNG View Post
Ah cool, I understand now.

Thanks once again !

Also I tried running the program without the "void" in between the brackets "int main( )" and it seemed to work fine, what is the reason for this?
Hey sorry for the late response.

"int main(void)" is just a c99 standard which means it's universally accepted by any compiler...

although almost every compiler accepts "int main()" since void is assumed because no command line arguments are needed..

"int main(int argc, char **argv)" is also the appropriate c99 standard way of using command line arguments...

Th program below is designed to state whether the integer entered is an Even or Odd number. I just thought there might be a better and more proffesional way of writting it?
The link you posted doesn't work. But THE BEST way to check for even and odd numbers without going outside the C standard library is to divide by 2 using modulo... module is just "%" and it returns the remainder... so odd will return 1 and even will be 0..

here's a quick example
#include <stdio.h>

int main(void)
{
    int idx = 0;

    for(idx; idx <= 10; idx++){
        if((idx % 2) == 0) {
            printf("%d = even\n", idx); 
        }else{
            printf("%d = odd\n", idx);
        }
    }
    
    getchar();
    return 0;
}
anyways im just coming off a 3 hour shroom trip n im still feeling a bit magical at this point.. so if this post was confusing it was the magic ;)


Register to reply

Related Discussions
Help! need to write fortran program Programming & Computer Science 8
Write a program to simluate multi object Advanced Physics Homework 7
SML compiler? Programming & Computer Science 1
Program to write neat equations and problems General Math 4
Robot uses four servos to walk Computing & Technology 1