What is an easy way to write a C program for creating a table using for loops?

  • Thread starter Dili
  • Start date
  • Tags
    Program
In summary: For( i=0; i<=num[i]; i++ ) { }In summary, the programmer is trying to get a table that looks like this:01234567891011121314151617181920
  • #1
Dili
13
0
I need to write a c program to get a table like this
______0 ________1 _________2________3 ...(underscore for presentation only)
10 sqrt(0*10) sqrt(1*10)
11
12
..
..

i know how to get the first row while saving it in an array.
but i can't find how to write the rest of the lines.
is tehre a easy way using only for loops?
here is a part of the code I've written

#include<stdio.h>
#include<math.h>
int main()
{
int i,j,num[20];
printf("\t");
for(i=0;i<9;i++)


num=i;
{
printf("%d\t",num);
}
 
Last edited:
Technology news on Phys.org
  • #2
What are you trying to achieve ?
Draw out on paper ( or in excel) what you want in each cell.
Work out what formula calcualtes these numbers
Write the two loops ( one to loop over columns and one over rows)
 
  • #3
Not sure why you'd need to use an array to store your values, unless you plan to use them later. Even if you were doing that, wouldn't a 2D array make more sense? Then, the easiest way would be to create a large enough array so that you just ignore the first 10 rows. Then, assuming rows = i and cols = j, you can do

num[j] = sqrt(i*j)

in a nested for loop. Use some simple arithmetic if you don't want to waste space in the array.

I think this should get you what you're after (at least without writing the program for you).
 
  • #4
Assuming this is the table you want...

Code:
[FONT="Courier New"]
    |      0       |      1       |       2      |     3
-----------------------------------------------------------
10  |  sqrt(0*10)  |  sqrt(1*10)  |  sqrt(2*10)  |
11  |  sqrt(0*11)  |  sqrt(1*11)  |  sqrt(2*11)  |
12  |  sqrt(0*12)  |  sqrt(1*12)  |  sqrt(2*12)  |
13  |[/FONT]
 
  • #5
This looks like homework, so I would suggest:
1. your prof wants you to learn about printf() formatting specifically how to make a field a fixed width - eg printf("%6.3f", sqrt( of something) );
2. your prof wants you to see that you can sprintf the same way as printf, except to build a string, not print right away.
3. you can use a for( ) loop to build the body of the table.
 

Related to What is an easy way to write a C program for creating a table using for loops?

What is a C program?

A C program is a computer program written in the C programming language. It is a high-level programming language that is commonly used for system programming, game development, and other types of software development.

What is the difference between C and other programming languages?

C is a low-level programming language, meaning that it provides direct access to hardware and system resources. Other high-level programming languages, such as Java and Python, provide a more abstracted and simplified way of programming.

Why is C considered a powerful language?

C is considered a powerful language because it allows for direct manipulation of hardware and memory, making it efficient and fast. It also has a wide range of features, including low-level memory management and a large standard library, making it versatile and suitable for a variety of applications.

What are the basic components of a C program?

A C program typically consists of a main function, variables, control structures (such as if/else statements and loops), and functions. The main function is the starting point of the program and contains the instructions that will be executed. Variables are used to store data, while control structures are used to control the flow of the program. Functions are blocks of code that can be called multiple times within a program.

What are the benefits of using C for programming?

There are several benefits of using C for programming, including its efficiency, speed, and portability. C programs can be compiled to run on different operating systems, making it a versatile language. Additionally, its low-level nature allows for precise control over system resources, making it a popular choice for developing system software.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
981
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
760
  • Programming and Computer Science
Replies
4
Views
915
Back
Top