473,383 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Format output into table

Hi, I have the following code all complete and executes precisely.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7.  
  8.  
  9.  
  10. double gravityFactor(double mass, double radius);
  11. double escapeVelocity(double mass, double radius);
  12.  
  13.  
  14. int main(void)
  15. {
  16.      double mass, radius, gf, ev;
  17.      int unit;
  18.  
  19.      double mass_pl[] = {3.3022e23, 4.8685e24, 5.9736e24, 7.3477e22, 6.4185e23,1.8986e27, 5.6846e26,
  20.                         8.6810e25, 1.0243e26};
  21.      double rad_pl[] = {2439700, 6051800, 6371000, 1737100, 3396200, 71492000, 60268000, 25559000, 
  22.                        24764000};     
  23.  
  24.       do
  25.       {
  26.           printf("Select 1 for escape velocities and gravities factor.Select 2 to input own values: ");
  27.           scanf("%d", &unit);
  28.       }while(unit !=1||unit !=2);    
  29.       if(unit==1)  
  30.       {
  31.          gf=((mass_pl[0]/mass_pl[2])/((rad_pl[0]/rad_pl[2])*(rad_pl[0]/rad_pl[2])));
  32.          printf("The gravity factor of Mercury is: %.2f\n", gf);
  33.  
  34.          gf=((mass_pl[1]/mass_pl[2])/((rad_pl[1]/rad_pl[2])*(rad_pl[1]/rad_pl[2])));
  35.          printf("The gravity factor of Venus is: %.2f\n", gf);
  36.  
  37.          gf=((mass_pl[2]/mass_pl[2])/((rad_pl[2]/rad_pl[2])*(rad_pl[2]/rad_pl[2])));
  38.          printf("The gravity factor of Earth is: %.2f\n", gf);
  39.  
  40.          gf=((mass_pl[3]/mass_pl[2])/((rad_pl[3]/rad_pl[2])*(rad_pl[3]/rad_pl[2])));
  41.          printf("The gravity factor of the Moon is: %.2f\n", gf);
  42.  
  43.          gf=((mass_pl[4]/mass_pl[2])/((rad_pl[4]/rad_pl[2])*(rad_pl[4]/rad_pl[2])));
  44.          printf("The gravity factor of Mars is: %.2f\n", gf);
  45.  
  46.          gf=((mass_pl[5]/mass_pl[2])/((rad_pl[5]/rad_pl[2])*(rad_pl[5]/rad_pl[2])));
  47.          printf("The gravity factor of Jupiter is: %.2f\n", gf);
  48.  
  49.          gf=((mass_pl[6]/mass_pl[2])/((rad_pl[6]/rad_pl[2])*(rad_pl[6]/rad_pl[2])));
  50.          printf("The gravity factor of Saturn is: %.2f\n", gf);
  51.  
  52.          gf=((mass_pl[7]/mass_pl[2])/((rad_pl[7]/rad_pl[2])*(rad_pl[7]/rad_pl[2])));
  53.          printf("The gravity factor of Uranus is: %.2f\n", gf);
  54.  
  55.          gf=((mass_pl[8]/mass_pl[2])/((rad_pl[8]/rad_pl[2])*(rad_pl[8]/rad_pl[2])));
  56.          printf("The gravity factor of Neptune is: %.2f\n", gf);
  57.  
  58.          ev =(sqrt((2*(6.67428e-11)*mass_pl[0])/rad_pl[0]))/1000; 
  59.          printf("The escape velocity of Mercury is: %.1f km/s\n", ev);
  60.  
  61.          ev =(sqrt((2*(6.67428e-11)*mass_pl[1])/rad_pl[1]))/1000; 
  62.          printf("The escape velocity of Venus is: %.1f km/s\n", ev);
  63.  
  64.          ev =(sqrt((2*(6.67428e-11)*mass_pl[2])/rad_pl[2]))/1000; 
  65.          printf("The escape velocity of the Earth is: %.1f km/s\n", ev);
  66.  
  67.          ev =(sqrt((2*(6.67428e-11)*mass_pl[3])/rad_pl[3]))/1000; 
  68.          printf("The escape velocity of the Moon is: %.1f km/s\n", ev);
  69.  
  70.          ev =(sqrt((2*(6.67428e-11)*mass_pl[4])/rad_pl[4]))/1000; 
  71.          printf("The escape velocity of Mars is: %.1f km/s\n", ev);
  72.  
  73.          ev =(sqrt((2*(6.67428e-11)*mass_pl[5])/rad_pl[5]))/1000; 
  74.          printf("The escape velocity of Jupiter is: %.1f km/s\n", ev);
  75.  
  76.          ev =(sqrt((2*(6.67428e-11)*mass_pl[6])/rad_pl[6]))/1000; 
  77.          printf("The escape velocity of Saturn is: %.1f km/s\n", ev);
  78.  
  79.          ev =(sqrt((2*(6.67428e-11)*mass_pl[7])/rad_pl[7]))/1000; 
  80.          printf("The escape velocity of Uranus is: %.1f km/s\n", ev);
  81.  
  82.          ev =(sqrt((2*(6.67428e-11)*mass_pl[8])/rad_pl[8]))/1000; 
  83.          printf("The escape velocity of Neptune is: %.1f km/s\n", ev);
  84.  
  85.  
  86.  
  87.       }  
  88.           else if(unit==2)
  89.           {
  90.  
  91.               do
  92.               {
  93.                   printf("Please input the mass of your stellar body: ");
  94.                   scanf("%lf", &mass);
  95.  
  96.  
  97.                   printf("Please indicate the radius of your stellar body: ");
  98.                   scanf("%lf",&radius);
  99.  
  100.                   gravityFactor(mass, radius); 
  101.  
  102.  
  103.                   escapeVelocity(mass, radius);
  104.  
  105.  
  106.                   printf("Please select 1 to calculate another planet. Press any number key to terminate: ");
  107.                   scanf("%d", &unit);
  108.  
  109.                }while(unit==1);
  110.           }
  111.  
  112.   return 0;
  113.  
  114.  
  115. }
  116.  
  117. double gravityFactor(double mass, double radius)
  118. {       
  119.    double gf;    
  120.    gf =((mass/5.9736e24)/((radius/6371000)*(radius/6371000)));  
  121.    printf("The gravity factor of this stellar body is: %.2f\n", gf);
  122.    return gf;     
  123. }  
  124.  
  125.  
  126. double escapeVelocity(double mass, double radius)
  127. {    
  128.    double ev;     
  129.    ev =(sqrt((2*(6.67428e-11)*mass)/radius))/1000; 
  130.    printf("The escape velocity of this stellar body is: %.2f km/s\n", ev);
  131.    return ev;    
  132. }     
  133.  
  134.  
  135.  

I need to know how to display all the results from the if(unit==1) section of the code in a table format ?
Apr 8 '10 #1
1 3314
Banfa
9,065 Expert Mod 8TB
You haven't really given the format of the table you want but just output each value with a tab '\t' character between each one or alternitively print each value with a fixed width specifier and a following space

Expand|Select|Wrap|Line Numbers
  1.          printf("The escape velocity of Mars is: %8.1f km/s\n", ev);
  2.  
Printing a table is just a matter of outputing tabs an spaces so that your columns of figures line up on the screen. No special functions required.
Apr 8 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jim | last post by:
im using asp.net, C# to enter data into a table in sql server...however im getting this error: Input string was not in a correct format. Description: An unhandled exception occurred during the...
1
by: Query Builder | last post by:
I am new to BCP. Can anyone help me understand this? I tried searching the BOL but it doesnt show much help on syntex. Lets say I have a simple table and want to out put the records into a file....
2
by: Paolo Pignatelli | last post by:
I am trying to get an output/file like this (below) in an XML file (MyXmlFile.xml) (which I will use for a slide show) -- <gallery timer="3" order="sequential" fadetime="2" looping="yes"...
6
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string...
8
by: bienwell | last post by:
Hi, I have a problem of displaying data bound by a datalist control. In my table, I have a field Start_date which has Short Date data type. I tried to update this field by Current Date. After...
2
by: PK | last post by:
Hi All, i got few questions to ask regarding about publishing dataset to html format !!! ------------------- PublishHTML("C:\Inetpub\wwwroot\Report.htm", myDataSet1.Tables("table01")) ...
9
by: seep | last post by:
hi i m finding following error on the code that i wants to use to get all record from table via store procedure with paging. the error is : Input string was not in a correct...
8
by: yashiro | last post by:
Hello i Have a database in SQLserver. I am using php to connect to it and to retrieve data from that database. I have a table with a column (DATATIME) where i do : $result =...
5
by: The Pipe | last post by:
Hello I have a table that I need to export to a .asc file format. I have had several attempts at getting this to work but with no luck. Basically the file would need to have every record...
22
by: sivadhanekula | last post by:
Hello Everyone A quick and direct question: I need a C/C++ program to extract the data from the database and the output should be in CSV "Comma Separated Value" format. Briefly: I will be given a...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.