View Single Post
Old 09-13-2014, 12:58 AM   #8
REACTOR
Question the trends!
 
REACTOR's Avatar
 
Join Date: Apr 2011
Location: Malaysia
Posts: 1,162
Default Re: Problem: Functions In C

Quote:
Originally Posted by SpaceManiac View Post
Confusion about where it was supposed to go, most likely. I imagine Making Dumb Games is good though.

Actual question answer: you're experiencing the fun of trying to compile multiple C source files into one program for the first time! (It's not that bad really).

I imagine you're trying a sequence of commands something like this, perhaps with more options:
Code:
gcc main.c
gcc fmin.c
What this tells the compiler to do is to try and make a complete program using only "main.c" and another one using only "fmin.c". Unfortunately, each file contains only part of the complete program - one is missing the definition (the code body) of "fminimum", and the other is missing the "main" function.

To compile both .c files at once into a single program, you want a command sort of like...
Code:
gcc main.c fmin.c
This tells the compiler to compile both source files as a single program, which is what you want. Now the finished program contains both a "main" function and the definition of the "fminimum" function.

There are more advanced techniques for compiling projects with many many source files rather than just a few but this should get you started.
Your advice worked very well, and gave me more of a clue as to what I should do next.
I compiled all my three functions along with my main one, and also successfully built all four files together. No problems so far.

But for some mysterious reason, when I run my program and get to the part where it does the fminimum operation, it just shows 0 even though the smallest number I typed in the array was 4. I tried it several times and so far, the same problem cropped up, 0, 0, 0...

I was hoping someone could point me the way to my bug, because I am really stumped at what I am doing wrong... Here are the files:

shw_ufo.c
Code:
/*
 * This program records the number of UFO sightings per day of a month.
 * 
 * Date: 7th August 2014
 * Author: Sebastian
 * 
 */
 
#include <stdio.h>
#include "fmax.h"
#include "fmin.h"
#include "fsum.h"

int main()

{
    
    const int MAX = 102;
    
    int ufo_table[MAX]; 
    int days_in_m = 0;
    int day_of_m = 1;
    int ufo_per_day = 0;
    
    int max = 0;
    int min = 0;
    int sum = 0;
    
    printf("\n");
    
    do
    {
        
    printf("UFO sighting recording.\n");
    printf("Please enter the number of days in your selected month.\n");
    printf("Valid number of days are from 1 to 100!\n==> ");
    scanf("%d", &days_in_m); 
    
    /*Validating number!*/
    
        if ( days_in_m < 1 || days_in_m > 100)
        {
            
            printf("\n*** \nNot a valid number, try again\n ***\n");
        };
        
    
    } while ( days_in_m < 1 || days_in_m > 100);
    
    /*Now we get the numbers of UFOs.*/
    
    do
    {
        
        printf("\n");
        printf("Enter number of UFO sightings for day %d.\n", day_of_m);
        printf("Valid numbers are from 0 to 999.\n==> ");
        scanf("%d", &ufo_per_day);
        
        /*Validating number!*/
        
        if ( ufo_per_day > -1 && ufo_per_day < 1000 )
        {
            
            ufo_table[day_of_m] = ufo_per_day; 
            day_of_m++;
            
        }
        
        else
        {
            
            printf("\n*** \nNot a valid number, try again\n ***\n");
            
        };
    
    } while ( day_of_m <= days_in_m );
    
    /*Assigning a specific value to the last index of the table to denote the end.*/
    
    day_of_m = day_of_m + 1;
    ufo_table[day_of_m] = -1;
    
    /*The last stage of the program, printing out all the values in the table!*/
    
    printf("\nNow printing out the number of UFO sightings per day which you have entered!\n\n");
    
    for ( day_of_m = 1; day_of_m <= days_in_m; day_of_m++ )
    {
        
        printf("Day[%3d] => %3d Sighting[s].\n\n", day_of_m, ufo_table[day_of_m]);
        
    };    
    
    printf("Extra statistics:\n");
    
    /*Finding the minimum number of UFO sightings entered.*/
    
    min = fminimum ( ufo_table );
    
    /*
    min = ufo_table[1];
    
    for ( day_of_m = 1; day_of_m <= days_in_m; day_of_m++ )
    {
        if (ufo_table[day_of_m] < min )
        min = ufo_table[day_of_m];
    };
    */
    
    printf("\n");
    printf("Minimum number of UFO sightings is [%3d].", min);
    
    /*Finding the maximum number of UFO sightings entered.*/
    
    max = fmaximum ( ufo_table );
    
    /*for ( day_of_m = 1; day_of_m <= days_in_m; day_of_m++ )
    {
        if ( ufo_table[day_of_m] > max )
        max = ufo_table[day_of_m];
    };
    */
    
    printf("\n");
    printf("Maximum number of UFO sightings is [%3d].", max);
    
    /*Adding the total sum of all UFO sightings.*/
    
    sum = findsum ( ufo_table );
    
    /*
    for ( day_of_m = 1; day_of_m <= days_in_m; day_of_m++ )
    {
        sum = sum + ufo_table[day_of_m];
    };
    */
    
    printf("\n");
    printf("Total sum of all UFO sightings is [%3d].", sum);
        
    printf("\n\n End of program! \n");
    
return 0;

};
fmin.c
Code:
/*
 * Author: Sebastian
 * Date: 26/AUG/2014
 * 
 * Input: An array of integers.
 * Output: The smallest number found from the array.
 * Description: This functions assigns a given array of values to a specific table, it then goes through a loop which
 * determines which value out of all the other values, is the smallest, and returns it.
 * 
 */

#include "fmin.h" 

int fminimum ( int table[] )
{

    int min = 0;
    int tableindex = 0;
    
    min = table[1];
    
    for ( tableindex = 1; table[tableindex] != -1; tableindex++ )
    {
        
        if ( table[tableindex] < min )
        {    
            
            min = table[tableindex];
            
        };
            
    };
    
    return (min);

};
fmin.h
Code:
/*
 * Author: Sebastian
 * Date: 26/AUG/2014
 * 
 * Input: An array of integers.
 * Output: The smallest number found from the array.
 * Description: This functions assigns a given array of values to a specific table, it then goes through a loop which
 * determines which value out of all the other values, is the smallest, and returns it.
 * 
 */

int fminimum ( int table[] );
fmax.c
Code:
/*
 * Author: Sebastian
 * Date: 26/AUG/2014
 * 
 * Input: An array of integers.
 * Output: The largest number found from the array.
 * Description: This functions assigns a given array of values to a specific table, it then goes through a loop which
 * determines which value out of all the other values, is the largest, and returns it.
 * 
 */

#include "fmax.h"

int fmaximum ( int table[] )
{
    
    int max = 0;
    int tableindex = 0;
    
    for ( tableindex = 1; table[tableindex] != -1; tableindex++ )
    {
        
        if ( table[tableindex] > max )
        {
            
            max = table[tableindex];
            
        };
            
    };
    
    return (max);
    
};
fmax.h
Code:
/*
 * Author: Sebastian
 * Date: 26/AUG/2014
 * 
 * Input: An array of integers.
 * Output: The largest number found from the array.
 * Description: This functions assigns a given array of values to a specific table, it then goes through a loop which
 * determines which value out of all the other values, is the largest, and returns it.
 * 
 */

int fmaximum ( int table[] );
fsum.c
Code:
/*
 * Author: Sebastian
 * Date: 26/AUG/2014
 * 
 * Input: An array of integers.
 * Output: The sum of all the values in the array.
 * Description: This functions assigns a given array of values to a specific table, it then goes through a loop which
 * adds all the given values in the array up, and returns it.
 * 
 */

#include "fsum.h"

int findsum ( int table[] )
{

    int sum = 0;
    int tableindex = 0;

    for ( tableindex = 1; table[tableindex] != -1; tableindex++ )
    {
        
        sum = sum + table[tableindex];
        
    };
    
    return (sum);
    
};
fsum.h
Code:
/*
 * Author: Sebastian
 * Date: 26/AUG/2014
 * 
 * Input: An array of integers.
 * Output: The sum of all the values in the array.
 * Description: This functions assigns a given array of values to a specific table, it then goes through a loop which
 * adds all the given values in the array up, and returns it.
 * 
 */

int findsum ( int table[] );
The two other custom functions, fmaximum, findsum, seem to work, but the fminimum function seems to have a problem which I don't know how to fix.

Last edited by REACTOR; 09-13-2014 at 01:06 AM. Reason: Oops, Wrong Files
REACTOR is offline   Reply With Quote