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

  • Thread starter Thread starter Dili
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around writing a C program to create a table using for loops, specifically focusing on generating a table of square roots based on a formula. Participants explore various approaches to implement the program, including the use of arrays and formatting output.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant seeks a method to generate a table using for loops, mentioning they can create the first row but struggle with subsequent rows.
  • Another participant suggests drawing the desired table layout on paper or in Excel to clarify the structure and formulas needed for calculations.
  • A different participant questions the necessity of using an array, proposing that a 2D array might be more appropriate for storing values, and suggests using a nested for loop to calculate values with the formula sqrt(i*j).
  • One participant provides a visual representation of the table structure they believe the original poster is aiming for, outlining how the square root values would be organized.
  • Another participant implies that the task may be homework, recommending that the original poster focus on specific aspects of C programming, such as formatting output with printf and using loops to build the table body.

Areas of Agreement / Disagreement

Participants express various approaches and suggestions, but no consensus is reached on a single method or solution. Different opinions on the use of arrays and formatting indicate a lack of agreement on the best approach.

Contextual Notes

Some participants note the importance of understanding output formatting and the potential use of different data structures, but the discussion does not resolve the specific implementation details or assumptions about the program's requirements.

Dili
Messages
13
Reaction score
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
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)
 
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).
 
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  |
 
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
14
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K