Creating Functions For My Program

This is a discussion on Creating Functions For My Program within the C Programming forums, part of the General Programming Boards category; Hi everyone, I need some help for an assignment...I have completed the program and it all works, however for a ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    Creating Functions For My Program

    Hi everyone,

    I need some help for an assignment...I have completed the program and it all works, however for a previous (easier) assignment I created the program all in main then removed parts and created functions to make the program more desirable. I am trying to do it for this assignment which basically multiplies 2 matrices and outputs the resultant, but it is proving rather difficult.

    Firstly, I'd like to make a function for the chk statement in my program as below;

    Code:
    int main (void){
        int MXA[4][4], MXB[4][4], MLT[4][4], RowA, ColA, RowB, ColB;
        int i, j, k;
        char vld[80], c;
        int chk;
        
        /* Intro */
        printf("                             Matrix Multiplier\n");
        printf("                             _________________\n");
        
        printf("\n    Welcome to the Matrix Multiplier.  This program is designed to output \n             the result of multiplying two, 4 X 4 matrices.\n");
        printf("\n_______________________________________________________________________________\n");
        /*END Intro */
        
        /*Matrix A*/  
    
            printf("\n  Create Size of Matrix A [MAX 4 X 4 Matrix]:\n");
            
            do {
            
                  
            printf("\n             Number of Rows [R] = ");
            
            gets(vld);
            chk = sscanf(vld, "%d",&RowA);
            
            if (chk != 1)
                   { printf ("\n             ERROR; You have entered a character.  Please Enter a number!\n"); }
                   else
                   if (RowA < 0 || RowA > 4)
                   { printf ("\n             You have entered a number out of range.\n             Please re-enter!\n"); }
                
                }   while (RowA < 0 || RowA > 4);
                
            
            do {                
            printf("\n          Number of Columns [C] = ");
            
            gets(vld);
            chk = sscanf(vld, "%d",&ColA);
            
            if (chk != 1)
                   { printf ("\n             ERROR; You have entered a character.  Please Enter a number!\n"); }
                   else
                   if (ColA < 0 || ColA > 4)
                   { printf ("\n             You have entered a number out of range.\n             Please re-enter!\n"); }
                
                }   while (ColA < 0 || ColA > 4);
        /*End Matrix A*/
    The chk statement makes the program more robust as to warn the user that a character has been entered. However, as you can see I have used it twice here, and a few more times in the rest of the program although I haven't shown it here. I'd like to make this a function so its cleaner in main. I have tried various ways but no luck at all.

    So basically, I'd like to chk loop bit to be in a function and call upon this function when I need it.

    Any help would be appreciated!!
    Thank you!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Don't use gets() -- it is depreciated, meaning the only reason it continues to exist is to accommodate (very) old code. Use fgets() instead.

    You actually don't need to use gets or fgets. You can just use scanf().

    So the check would go into a function with this prototype:

    Code:
    int getValue();
    // or
    int getValue(int min, int max);
    Which uses scanf() in a check loop and doesn't return until it gets a proper number. You call it like this:

    Code:
    int RowA = getValue(0, 4)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    I kind of understand that....but I'm not too sure what you mean by the check loop would go into the function getValue? Would I put the current check loop structure I have made into the function?

    I know this is a wild shot but I just tried to create a mini program to see how this works...
    Code:
    #include <stdio.h>#include <math.h>
    
    
    int getValue()
    {   int chk, n, value;
        
        
        if (chk != 1)
                   { printf ("\n             ERROR; You have entered a character.  Please Enter a number!\n"); }
                   else
                   if (n < 0 || n > 4)
                   { printf ("\n             You have entered a number out of range.\n             Please re-enter!\n"); }
                     
        
        return (value);
    }
    
    
    int main (void)
    {
        int a, b, c;
        
         
            a = getValue(0,4);
            printf("\n  Create Size of Matrix A [MAX 4 X 4 Matrix]:\n");
                             
            printf("\n             Number of Rows [R] = ");
            
            scanf("%d",&a);
            
            b = getValue(0,4);
            printf("\n  Create Size of Matrix B [MAX 4 X 4 Matrix]:\n");
                             
            printf("\n             Number of Rows [R] = ");
            
            scanf("%d",&b);
            
        system("PAUSE");
        return (0);
    }
    I know it doesn't work, but is this the kind of thing you were talking about? And is it fixable so that it actually works as intended?
    Last edited by nelly26; 04-02-2012 at 09:11 PM.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    It means that right now, in getValue() you are trying to get the user to input a correct number, and if he doesn't you decide to return uninitialized variable value which contains garbage, instead of looping until you receive correct input.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 01:17 PM
  2. creating and using functions
    By moy18 in forum C Programming
    Replies: 2
    Last Post: 11-01-2007, 05:26 AM
  3. creating and using functions
    By moy18 in forum C Programming
    Replies: 2
    Last Post: 10-31-2007, 10:09 PM
  4. Creating Functions
    By Rune Hunter in forum C# Programming
    Replies: 5
    Last Post: 03-03-2005, 07:22 PM
  5. Help with creating functions
    By OmnipotentCow in forum C Programming
    Replies: 8
    Last Post: 06-27-2003, 11:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21