Thread: Extreme Newbie...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    11

    Extreme Newbie...

    Ok, I am very new to C programming. I have tried to figure this little peice out for like 6 hours now. I just don't quite understand the syntax for user defined functions.

    I think I have the code within the functions correct however I just can't get it to output. But I can't be for certain, however, I think my sorting function is correct since I got it from a book.

    A nudge in the right direction or a little advice would be great!

    My code:
    Code:
    // F to C Conversion and Sorting
    
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #define n 24
    
    int main()
    {
    	double f_to_c(double f_temp);
    	double temps[n];
    	double abe(int num_temps, double temps[]);
    	double average(int num_temps);
    	int num_temps;
    	int get_temps(int num_temps, double temps[]);
    	void temp_sort(double temps[],int num_temps);
    	double temp_output(double avg, double high, double low, int above,
    		double temps[], int below, int equal, int more100, int more32,
    		int less32, int num_temps);
    	double avg, high, low;
    	int above, below, equal, more100, more32, less32;
    
    	get_temps(num_temps,temps[n]);
            temp_sort(temps[n],num_temps);
    	abe(num_temps,temps[n]);
           average(num_temps);
           temp_output(avg,high,low,above,temps[n],below,equal,more100
                               ,more32,less32,num_temps);
    
    	return 0;
    }
    
    // Get temperatues
    int get_temps(int num_temps, double temps[])
    {
    	int i;
    
    	printf("How many temperatures are you going to enter:");
    	scanf("%i",&num_temps);
    
    	for (i=0;i<=num_temps-1;i++) {
    
    		printf("Enter a temperature (-150 to 150):");
    		scanf("%lf",&temps[i]);
    
    		if (temps[i] > 150 || temps[i] < -150) {
    
    			printf("Invalid range, try again (-150 to 150):");
    			i--;
    		}
    	}
    	return num_temps;
    }
    
    // Convert C to F
    double f_to_c(double f_temp)
    {
    	double cel;
    
    	cel = ((double)f_temp - 32) * 5 / 9;
    
    	return cel;
    
    }
    
    // Get average
    double average(int num_temps)
    {
    	int i;
    	double total=0;
    	double temps[24];
    	double avg;
    
    	for (i=0;i<=num_temps-1;i++){
    
    		total = total + temps[i];
    
    	}
    
    	avg = total / num_temps;
    
    	return avg;
    }
    
    // Above, below, equal
    int abe (int num_temps, double temps[])
    {
    	double avg;
    
    	int i,above=0,below=0,equal=0;
    
    	for (i=0;i<=num_temps-1;i++) {
    
    		if (temps[i] > avg) {
    			above++;
    		} else if (temps[i] < avg) {
    			below++;
    		} else {
    			equal++;
    		}
    	}
    	return above,below,equal;
    }
    
    // Ranges
    double temp_ranges(int num_temps)
    {
    
    	int i,more100,less32,more32;
    
    	double high,low,temps[24];
    
    	for (i=0;i<=num_temps-1;i++) {
    
    		if(temps[i] < low) {	
    			low = temps[i];
    		}
    		if(temps[i] > high) {
    			high = temps[i];
    		}
    		if(temps[i] >= 100) {
    			more100++;
    		}
    		if(temps[i] <= 32) {
    			less32++;
    		}
    		if(temps[i] > 32 && temps[i] < 100) {
    			more32++;
    		}
    	}
    	return high,low,more100,less32,more32;
    }
    
    // Sort Array
    
    void temp_sort(double temps[],int num_temps)
    {
    	int k,j,m;
    	double hold;
    	m = k;
    
    	for (k=0;k<=num_temps;k++) {
    
    		if(temps[j] < temps[m])
    			m = j;
    		hold = temps[m];
    		temps[m] = temps[k];
    		temps[k] = hold;
    	}
    	return;
    }
    
    // Display temperatures
    
    double temp_output(double avg, double high, double low, int above,
    				   double temps[], int below, int equal, int more100,
    				   int more32, int less32, int num_temps)
    {
    
    	int i;
    
    	printf("                   Fahr:     Cels:\n");
    
    	for(i=0;i<=num_temps;i++) {
    
    		printf("                   %.1lf     ",temps[i]);
    
    		i++;
    
    		printf("%lf\n",temps[i]);
    	}
    
    	printf("                ---------------------\n");
    	printf("Average:            %.1lf     %lf\n\n",avg,f_to_c(avg));
    	printf("Low:                %.1lf     %lf\n\n",low,f_to_c(low));
    	printf("High:               %.1lf     %lf\n\n",high,f_to_c(high));
    	printf("Above Average:      %i\n\n",above);
    	printf("Below Average:      %i\n\n",below);
    	printf("Equal to Average:   %i\n\n",equal);
    	printf("100 F and Above:    %i\n\n",more100);
    	printf("> 32 F and < 100 F: %i\n\n",more32);
    	printf("32 F and Below:     %i\n\n",less32);
    }
    Sorry for not just posting code snippets, but at this point I'm not really sure exactly what is right and what is wrong.

  2. #2
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    your function definitions should be global. Instead of declaring them in the main() part, put them above it, like so:

    Code:
    double f_to_c(double f_temp);
    double temps[n];
    double abe(int num_temps, double temps[]);
    double average(int num_temps);
    //etc..
    
    int main()
    {
        double x;
        x = f_to_c(98.6);
        printf("%d", x);
    }
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    Alright, thanks.
    I made all my variables global now.

    The compiler errors I am getting are "functiion: incompatible types".
    And a few warnings "different types for formal and actual parameter".
    Using Visual Studio 6.

    Understanding the errors supplied by the compiler might also help me debug better.
    Last edited by lonewolf367; 11-07-2005 at 01:04 AM.

  4. #4
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    lets see the code
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    This is all I've changed so far:

    Code:
    double temps[N];
    int num_temps;
    double avg, high, low;
    int above, below, equal, more100, more32, less32;
    double f_to_c(double f_temp);
    double temps[N];
    int abe(int num_temps, double temps[]);
    double average(int num_temps);
    double temp_output(double avg, double high, double low, int above,
    		double temps[], int below, int equal, int more100, int more32,
    		int less32, int num_temps);
    void temp_sort(double temps[],int num_temps);
    double get_temps(int num_temps,double temps[]);
    
    int main()
    {
    
    	get_temps(num_temps,temps[N]);
    	temp_sort(temps[N],num_temps);
    	abe(num_temps,temps[N]);
    	average(num_temps);
    	temp_output(avg,high,low,above,temps[N],below,equal,more100,more32,less32,num_temps);
    
    	return 0;
    
    }
    Last edited by lonewolf367; 11-07-2005 at 01:12 AM.

  6. #6
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    what is N? what is num_temps? right now they are nothing. Instead of using global variables just use a number like:

    Code:
    int main()
    {
      num_temps = 10;
      double temps[10];
    
      get_temps(num_temps, temps);
      temp_sort(temps, num_temps); //THIS WILL NEVER WORK
      abe(num_temps, temps);
      //etc..
    }
    temp_sort would never work because after you leave the scope of get_temps, temps is nothing again. you could counter this by passing in a pointer or a reference to temps instead of using a copy of it.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    oh, i want to pass the array temps[] into the sorting algorithm. i thought arrays were passed by referance.

    and the num_temps is what the user imputs so that can chose the amount of numbers they would like to enter.

    Edit:
    Ok, I've created a pointer for num_temps. I still cannot run the sort however, I get a memory error when that tries to execute. And also, I tried creating a pointer for avg but... instead of giving me no values I'm getting garbage values.

    Getting closer...
    Last edited by lonewolf367; 11-07-2005 at 02:18 AM. Reason: Code update

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You pass arrays to functions like this:

    Code:
    get_temps(num_temps,temps);
    
    // not like this
    
    get_temps(num_temps,temps[N]);
    Sent from my iPadŽ

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    11
    Ok, thanks. I switched above,below,average, ect... to be entered into an additional array. That all works now.

    However, the sorting does not work. That is the only peice I'm now missing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 18
    Last Post: 11-04-2005, 02:41 PM
  2. Replies: 2
    Last Post: 12-09-2004, 02:59 PM
  3. Extreme newbie need help Installing Slackware 10.0
    By axr0284 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-09-2004, 01:46 PM
  4. Extreme Newbie
    By TheMetsAreBad in forum C++ Programming
    Replies: 14
    Last Post: 05-15-2003, 08:24 AM
  5. extreme newbie
    By sreetvert83 in forum C++ Programming
    Replies: 12
    Last Post: 03-31-2003, 06:36 PM