1 Replies - 2733 Views - Last Post: 13 November 2010 - 12:53 PM Rate Topic: -----

#1 c infant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-November 10

celcius to farenheit convertor with for loop

Posted 13 November 2010 - 09:19 AM

Hi all,
I am having a problem with this convertor.
My remit is:
Design a C program to solve the following problem:

A program is required that will convert degrees Celsius to degrees Fahrenheit. The user must enter a start and a finish value together with a step value. The program must convert all temperatures beginning with the start value at the given step interval.

Use the “for” statement for this problem.

Example Input:
Input the start value: 10
Input finish value: 16
Input step value: 2

Desired output:
Celsius Fahrenheit
10 50.0
12 53.6
14 57.2
16 60.8

The for statement syntax is confusing me atm, please help.

This is what I have so far:

// steped celcius to farenheit convertor
// Author c infant
// Date last modified 13/11/10 13:53


#include <stdio.h>
#include <stdlib.h>

main ()

{
            float c1,c2,step,celcius,farenheit;
     
            printf("Enter celcius value:");        // enter first variable and scan for input
            scanf("%f",&c1);
     
            printf("Enter Finish value:");        // enter 2nd variable and scan for input
            scanf("%f",&c2);
     
            printf("Enter Step value:");           // enter step variable and scan for input
            scanf("%f",&step);
     
            printf("\tcelcius\t  farenheit\n");   // print celcius / farenheit headings
     
            for(c1+=step;c1<=c2)                 // looping conditions 
           
     { 
            farenheit=1.8*celcius+32            // manipulation of variables
            printf("\t%.1f\t",celcius);             // print results
            printf ("%.1f\t",farenheit);
     }                                         // end loop
     
            system("PAUSE");                   // pause to view answer
      
     }                                        // end program


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 13 November 2010 - 09:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: celcius to farenheit convertor with for loop

#2 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 232
  • Joined: 07-July 10

Re: celcius to farenheit convertor with for loop

Posted 13 November 2010 - 12:53 PM

Here's a modified version of your code. I fixed the for syntax, added error checks, fixed the text output, and made the formatting more consistent and readable. Good formatting is something a lot of new coders wrongly neglect.

// steped celcius to farenheit convertor
// Author c infant
// Date last modified 13/11/10 13:53


#include <stdio.h>
#include <stdlib.h>

int main()
{
    float c1, c2, step;

    printf("Enter start value: ");	// Put spaces after colon
    scanf("%f", &c1);

    printf("Enter finish value: ");
    scanf("%f", &c2);

    printf("Enter step value: ");
    scanf("%f", &step);

    printf("celcius\tfarenheit\n");

    if (step <= 0.0f)
    {
    	// Error: if the loop begins it will never exit. Report and exit or correct and continue
    }

    if (c1 > c2)
    {
    	// Error as above
    }

	// Format for for statement is for (init-expr; condition-expr; increment-expr)
	//  v- Need a null statement here to indicate no init expression
    for(; c1<=c2; c1+=step)
    {
    	// In general variables should be in as close a scope as possible
        float farenheit = 1.8f * c1 + 32.0f;		// c1 holds the current celsius value, not the celsius variable you had which was not initialised
        printf("%.1f\t%.1f\n", c1, farenheit);
    }

    system("PAUSE");

    return 0;
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1