user2985811
user2985811

Reputation: 21

having an issue about the output in c programming ..

i'm having a problem on running the output after putting the input.. the output doesn't show after i put the variables and i don't know how to set the code .. so if you guys could help me with this, that would be grateful..

#include <stdio.h>
#include <conio.h>

int read_temps (float temps[]);
int hot_days (int numOfTemp, float temps[]);
int printf_temps (int numOfTemp, float temps[], int numOfHotDays);

int main (void) {
int index = 0;
float tempVal;
float temps[31];
int numOfTemp, numOfHotDays;

do {
    printf ("Enter the temperature:");
    scanf ("%f", &tempVal);
    if (tempVal!=-500.0) {
        temps[index] = tempVal;
        index++;
    }
} while (tempVal != -500.0);

return ;

{
    int i;
    int count = 0;
    for (i = 0; i < numOfTemp; i++) {
        if (temps[i] > 32.0)
            count++;
    }
    return count;
}

{
    float sum = 0.0;
    int i;
    printf ("\nInput Temperatures:");
    printf ("\n-------------------------");

    for (i = 0;i < numOfTemp; i++) {
        printf ("\nDay %d : %.2fF", i+1, temps[i]);
        sum = sum + temps[i];
    }
    printf ("\nNumber of Hot Days : %d", numOfHotDays);
    printf ("\nAverage Temperature: %.2f", sum/numOfTemp);
}

{
    clrscr ();
    numOfTemp = read_temps (temps);
    numOfHotDays = hot_days (numOfTemp, temps);
    clrscr ();
    printf_temps (numOfTemp, temps, numOfHotDays);
    getch ();
}
}

Upvotes: 0

Views: 376

Answers (4)

paxdiablo
paxdiablo

Reputation: 881113

That's actually a good effort. You have the code logic done perfectly, it's just that they're not arranged correctly.

You have the sub-functions that you want to call from main embedded within their own scope within main itself. That's not going to work well.

What you need to do is to first move them outside of main and add their declaration lines. Then you need to call them from main. In other words, something like this:

#include <stdio.h>

int read_temps (float temps[]);
int hot_days (int numOfTemp, float temps[]);
int printf_temps (int numOfTemp, float temps[], int numOfHotDays);

int main (void) {
    int index = 0;
    float temps[31];
    int numOfTemp, numOfHotDays;

    // The code to call them has been cleaned up.

    numOfTemp = read_temps (temps);
    numOfHotDays = hot_days (numOfTemp, temps);
    printf_temps (numOfTemp, temps, numOfHotDays);

    return 0;
}

// All the functions that were subsumed in main
//   have been separated and properly defined here.

int hot_days (int numOfTemp, float temps[]) {
    int i;
    int count = 0;
    for (i = 0; i < numOfTemp; i++) {
        if (temps[i] > 32.0)
            count++;
    }
    return count;
}

int printf_temps (int numOfTemp, float temps[], int numOfHotDays) {
    float sum = 0.0;
    int i;
    printf ("\nInput Temperatures:");
    printf ("\n-------------------------");

    for (i = 0;i < numOfTemp; i++) {
        printf ("\nDay %d : %.2fF", i+1, temps[i]);
        sum = sum + temps[i];
    }
    printf ("\nNumber of Hot Days : %d", numOfHotDays);
    printf ("\nAverage Temperature: %.2f\n", sum/numOfTemp);
}

int read_temps (float temps[]) {
    int index = 0;
    float tempVal;
    do {
        printf ("Enter the temperature:");
        scanf ("%f", &tempVal);
        if (tempVal!=-500.0) {
            temps[index] = tempVal;
            index++;
        }
    } while (tempVal != -500.0);
    return index;
}

I've basically performed the following steps:

  • Removed the non-standard stuff like conio.
  • Moved the embedded functions out to their proper place and given them the prototypes.
  • Move some variables from main to the other functions where they belong.
  • Tidied up some minor output things.
  • Tested it :-)

The transcript below shows a sample run:

Enter the temperature:3
Enter the temperature:4
Enter the temperature:5
Enter the temperature:6
Enter the temperature:50
Enter the temperature:-500

Input Temperatures:
-------------------------
Day 1 : 3.00F
Day 2 : 4.00F
Day 3 : 5.00F
Day 4 : 6.00F
Day 5 : 50.00F
Number of Hot Days : 1
Average Temperature: 13.60

Upvotes: 2

user
user

Reputation: 130

You have used return; before the function ends. That will create dead code, and so function will not work as expected.

Upvotes: 0

raof01
raof01

Reputation: 76

And you should not return nothing in function main()

Upvotes: 1

SLaks
SLaks

Reputation: 887215

 return ;

You just told your function to stop executing.

It's doing exactly what you told it to.

Upvotes: 3

Related Questions