By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,987 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,987 IT Pros & Developers. It's quick & easy.

how to tweak the code into modular programming?

P: 2
Hey guys,i am very very new to c programming and i really need your guys help,please.Below is the code that i use array to collect 5 integer and determine the minimum integer from the 5 integer using c language.

#include<stdio.h>
int main()
{
int i,min;
int number [5];
for(i=0;i<5;i++)
{
printf("enter number:");
scanf("%d",&number[i]);

}
for(i=0;i<5;i++)
{
if(number[i]<min)
{
min=number[i];
}
}
printf("the minimum value is %d",min);
}

AND HERE'S THE QUESTION
use functions to(c language) :
i.Get the 5 integers from user (Function : user_input)
ii.Find the smallest number (Function: find_smallest)
iii.Display the smallest number (Function: Main)

i kinda don't know how to start.
Apr 23 '17 #1
Share this Question
Share on Google+
3 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You kind of coded it already:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     user_input();
  4.     find_smallest();
  5.     printresult();
  6. }
  7.  
Step 1 would be to code these functions as empty functions and compile and run the program.

Step 2 would be to fill in the functions. Here you can decide to provide function arguments or have functions return values, like find_smallest() might return the smallest value which could then be passed to printresult() for display. Here is when you can use the code you have to go inside these functions. It's not going to be a perfect fit. You may need to do a little tailoring.

I would start by using global variables to avoid passing function arguments. After the program works, I would remove the global variables and replace them with local variables in main(). Use global variables only in development, never in a finished program.

BTW: In a modular system, main() cannot display the result. There is no way main() can know the display format. You need a function for that.

BTW2: In modular programming each function can do only one thing. If a function has to do two things then it calls two functions each of does only one thing.

Post again as you go along. I'm curious how you will do.
Apr 23 '17 #2

P: 2
thanks and heres what i have done,but at the place i make it bold,the code said it is redeclared as different symbol and i kinda don't know why,cause i put it integer all the way

#include<stdio.h>
int find_min(int);

int main()
{
int i,min;
int number [5];
int find_min;
for(i=0;i<5;i++)
{
printf("enter number:");
scanf("%d",&number[i]);
find_min=number[i];
printf("the minimum number is %d",min);
return 0;
}

int find_min(int number[i],int i)
{
int min;
for(i=0;i<5;i++)
{
if(number[i]<min)
{
min=number[i];
}
return find_min;
}

}
Apr 24 '17 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Your function prototype says find_min has one argument, an int.

Inside main() there is another find_min declared as an int variable.

The function in bold says find_min is a function with two arguments, an int and an int.

By the time the compiler got to your function definition it was confused, hence the error.

In any case, a compiler error at a line number means that when the compiler got there, there was an error. The location of the error is that line or any other line preceding up to the beginning f the code.

You should have only one find_min. All symbols must be unique.
Apr 24 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.