Thread: Creating Functions For My Program

1. 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. 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)`

3. 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?

4. 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.