C, compiler - write a table from a program

In summary, the program calculates the squares and cubes of integers from 0 to 10. It uses tabs (\t) to illustrate the data in the table.
  • #1
UNG
43
0
Hi,
I am trying to write a program on a C compiler and I am 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 I am not too sure which instructions are needed to show the information in a table.

Please help, thanks very much !
 

Attachments

  • Ques Example.jpg
    Ques Example.jpg
    11.3 KB · Views: 1,457
  • Example of.jpg
    Example of.jpg
    35.4 KB · Views: 699
Physics news on Phys.org
  • #2
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?
 
  • #3
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
 
  • #4
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?
 
  • #5
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 haven't been taught looping yet?
Any example of this?
 
  • #6
Hang on,
Do I just type the correct values under each header instead, without it actually being a flowing program.
 
  • #7
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.
 
  • #8
haha.
Yes that's right.

Um... I have only really mannaged to separate the three columns. But when I type in the numbers it won't show properly, as I am missing out a large part of the program.
 
  • #9
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.
 
  • #10
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
 
Last edited:
  • #11
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.
 
Last edited:
  • #13
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.

Code:
#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; 
}
 
  • #14
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 I am not sure why the "++" part is needed after the "<= 10; ix" bit.
?
 
  • #15
++ 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.
 
  • #16
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?
 
Last edited:
  • #17
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
 
  • #18
UNG said:
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
Code:
#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 I am just coming off a 3 hour shroom trip n I am still feeling a bit magical at this point.. so if this post was confusing it was the magic ;)
 
  • #19
NoUse said:
Code:
#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; 
}
Hey NoUse, why is it that when you add .0, or even . in front of f in the 2nd to last printf statement it removes the 0's after the decimal place for each number? I noticed that if you take out the .0, the output will be 0.000000, 1.000000, etc. And if you add the . or .0 before f it will just print 0, 1, etc. Also, I notcied that if you use int, instead of double the program just outputs all 0's, why is double needed? I know double is for double precision and that's it. Thanks!
-Matt
 
Last edited:

1. What is a C compiler?

A C compiler is a computer program that translates source code written in the C programming language into machine code, which can be executed by a computer. It is an essential tool for software development, as it allows developers to write programs in a high-level language and have them run on different platforms and architectures.

2. How does a C compiler work?

A C compiler works by taking in source code written in the C programming language and translating it into machine code. This process involves several stages, including lexical analysis, syntax analysis, semantic analysis, and code generation. The compiler also performs optimizations to improve the performance of the resulting machine code.

3. What is the purpose of writing a table from a program in a C compiler?

Writing a table from a program in a C compiler is a process known as symbol table generation. It involves creating a data structure that stores information about the symbols (variables, functions, etc.) used in the program. This table is used during the compilation process to resolve references to symbols and ensure the correctness of the resulting machine code.

4. How do you write a table from a program in a C compiler?

To write a table from a program in a C compiler, the compiler must first perform lexical analysis to break the source code into tokens. Then, it performs syntax analysis to ensure that the code follows the rules of the C programming language. Next, the compiler uses semantic analysis to create a symbol table and check for any errors. Finally, the compiler generates the machine code, including the necessary symbol references from the table.

5. What are some common errors that can occur when writing a table from a program in a C compiler?

Some common errors that can occur when writing a table from a program in a C compiler include syntax errors, such as missing semicolons or parentheses, and semantic errors, such as using an undefined variable or function. These errors can be identified by the compiler during the compilation process and must be fixed before the program can be successfully compiled and executed.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • STEM Academic Advising
Replies
12
Views
1K
Back
Top