Thread: help with arrays and equation

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    33

    help with arrays and equation

    I'm trying to convert values of an array from C to F.
    So far I have the user inputting values for each element (15 in the array which is defined). And I've figured out how to print each element, im just not sure how to apply the equation F = 32 + C*(180.0/100.0) so I can convert each element.


    Code:
    #include <stdio.h>#include <stdlib.h>
    #define COUNT 15
    
    
    int main(void){
    
    
    	int C, i;
    	int array[COUNT];
    	float F;
    	printf("Enter 15 temperatures in Celsius\n");
    	for(i=0;i<COUNT;i++){
    	scanf("%d", &array[i]);
    	}
    
    
        for(i=0;i<COUNT;i++){
            printf("%d\n", array[i]);
        }

  2. #2
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    You have to decide if you want to process decimal numbers. Both source and destination have to match type.

    A hint would be: If you can calculate F when C is given, as shown in your equation, it's your job to display this information to the user (reading the output from a CLI).
    This parameter is reserved

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    Quote Originally Posted by Ktulu View Post
    You have to decide if you want to process decimal numbers. Both source and destination have to match type.

    A hint would be: If you can calculate F when C is given, as shown in your equation, it's your job to display this information to the user (reading the output from a CLI).
    I'm not so worried about using decimals or not, as I can cast the results right?
    Just confused how I would apply the equation to each element in the array and then using the same code i have currently to output the elements

  4. #4
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi

    If you wanted to keep what you have... and you can work out float or int...and formatting...
    You could always do something like... array[i] = 32 + (array[i]*1.8);
    The expression gets solved before being assigned to the same array element that was holding the C deg value.

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    Quote Originally Posted by JohnGM View Post
    Hi

    If you wanted to keep what you have... and you can work out float or int...and formatting...
    You could always do something like... array[i] = 32 + (array[i]*1.8);
    The expression gets solved before being assigned to the same array element that was holding the C deg value.
    Tried this but got 0.00 for all the elements in my array

    Code:
        printf("All temperatures processed\n");
        for(i=0;i<COUNT;i++){
            array[i] = 32 + (array[i]*1.8);
            printf("%.2lf\n", array[i]);

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to change array to be an array of double too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,115
    Code:
    printf("%.2lf\n", array[i]);
    You created an array of ints. You are using the wrong conversion specifier to display them.

  8. #8
    Registered User
    Join Date
    Dec 2014
    Posts
    33
    thanks all. I figured it out
    I realized i did not have a second array for the converted values to be stored.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Pi equation
    By cwalton114 in forum C Programming
    Replies: 2
    Last Post: 04-30-2013, 09:07 AM
  2. Equation wrong
    By arnell22 in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2011, 01:31 PM
  3. PI Equation in C
    By wallysworld in forum C Programming
    Replies: 13
    Last Post: 10-23-2006, 08:12 PM
  4. equation
    By waltter in forum Windows Programming
    Replies: 1
    Last Post: 04-11-2006, 04:41 AM
  5. how get an equation ?
    By arian in forum C++ Programming
    Replies: 4
    Last Post: 01-16-2004, 04:40 AM