hello sorry in advance because i ask a very simple question, but i really ask for help, i have a school assignment, but i don't know how to solve it, please help
Stimulation
After you study program modularization, does the if else program below contain the process of modularization?
#include <stdio.h>
main(){
int a,b,c, max;
printf("enter value a :");scanf("%d",&a);
printf("enter value b :");scanf("%d",&b);
printf("enter value c :");scanf("%d",&c);
//checking 1 program module
if((a>b)&&(a>c))
{
max = a;
printf("highest value checking 1 : %d" ,max);
}else if(b > c)
{
max=b;
printf("highest value checking 1 : %d" ,max);
}else{
max=c;
printf("highest value checking 1 : %d" ,max);
}
printf("\n");
int number1, number2, number3;
number1=a;
number2=b;
number3=c;
//checking 2 main programs
if((number1>number2)&&(number2>number3)){
max = number1;
printf("highest score checking 2 : %d" ,max);
}else if(number2 > number3)
{
max=number2;
printf("highest score checking 2 : %d" , max);
}else{
max=number3;
printf(highest score checking 2 : %d" ,max);
}
}
Identification of problems.
Write down the hypothesis from the results of the if else program study. Is the hypothesis alternative (the if else program line is in accordance with the concept of modularization) or the hypothesis is a null hypothesis (the if else program is not in accordance with the concept of modularization). Explain the hypothesis in detail, and point out which line of if else program strengthens your hypothesis statement!
The program is not modular.
Here is the analysis:
The two if/else blocks are nearly identical.
The printf statements are replicated code.
The scanf calls are replicated code.
The code can only handle three numbers.
Amongst other things, modularization is a process of eliminating replicated/duplicated code that is similar and placing the common code in [smaller] functions.
A good maxim to follow is that a given function: should do one thing well.
We can modularize the code in several steps:
Move the first if/else block into a separate function (e.g. getmax)
Eliminate duplicate printf in getmax
Make getmax more general so it can handle the second if/else more easily
Convert second if/else block in main to use getmax
Put user input (printf/scanf) into common function
Convert program to use arrays rather than individual scalars. This is particularly evident with: int number1,number2,number3;. It can be simplified into: int number[3];
Convert program to prompt user for number of elements in the array
Move the first if/else block into a separate function:
#include <stdio.h>
// checking 1 program module
int
getmax(int a,int b,int c)
{
int max;
if ((a > b) && (a > c)) {
max = a;
printf("highest value checking 1 : %d\n", max);
}
else if (b > c) {
max = b;
printf("highest value checking 1 : %d\n", max);
}
else {
max = c;
printf("highest value checking 1 : %d\n", max);
}
return max;
}
int
main(void)
{
int a, b, c, max;
printf("enter value a :");
scanf("%d", &a);
printf("enter value b :");
scanf("%d", &b);
printf("enter value c :");
scanf("%d", &c);
getmax(a,b,c);
int number1, number2, number3;
number1 = a;
number2 = b;
number3 = c;
//checking 2 main programs
if ((number1 > number2) && (number2 > number3)) {
max = number1;
printf("highest score checking 2 : %d\n", max);
}
else if (number2 > number3) {
max = number2;
printf("highest score checking 2 : %d\n", max);
}
else {
max = number3;
printf("highest score checking 2 : %d\n", max);
}
return 0;
}
Eliminate duplicate printf in getmax:
#include <stdio.h>
// checking 1 program module
int
getmax(int a,int b,int c)
{
int max;
if ((a > b) && (a > c)) {
max = a;
}
else if (b > c) {
max = b;
}
else {
max = c;
}
printf("highest value checking 1 : %d\n", max);
return max;
}
int
main(void)
{
int a, b, c, max;
printf("enter value a :");
scanf("%d", &a);
printf("enter value b :");
scanf("%d", &b);
printf("enter value c :");
scanf("%d", &c);
getmax(a,b,c);
int number1, number2, number3;
number1 = a;
number2 = b;
number3 = c;
//checking 2 main programs
if ((number1 > number2) && (number2 > number3)) {
max = number1;
printf("highest score checking 2 : %d\n", max);
}
else if (number2 > number3) {
max = number2;
printf("highest score checking 2 : %d\n", max);
}
else {
max = number3;
printf("highest score checking 2 : %d\n", max);
}
return 0;
}
Make getmax more general so it can handle the second if/else more easily:
#include <stdio.h>
// checking 1 program module
int
getmax(const char *what,int a,int b,int c)
{
int max;
if ((a > b) && (a > c)) {
max = a;
}
else if (b > c) {
max = b;
}
else {
max = c;
}
printf("highest %s checking 1 : %d\n", what, max);
return max;
}
int
main(void)
{
int a, b, c, max;
printf("enter value a :");
scanf("%d", &a);
printf("enter value b :");
scanf("%d", &b);
printf("enter value c :");
scanf("%d", &c);
getmax("value",a,b,c);
int number1, number2, number3;
number1 = a;
number2 = b;
number3 = c;
//checking 2 main programs
if ((number1 > number2) && (number2 > number3)) {
max = number1;
printf("highest score checking 2 : %d\n", max);
}
else if (number2 > number3) {
max = number2;
printf("highest score checking 2 : %d\n", max);
}
else {
max = number3;
printf("highest score checking 2 : %d\n", max);
}
return 0;
}
Convert second if/else block in main to use getmax:
#include <stdio.h>
// checking 1 program module
int
getmax(const char *what,int a,int b,int c)
{
int max;
if ((a > b) && (a > c)) {
max = a;
}
else if (b > c) {
max = b;
}
else {
max = c;
}
printf("highest %s checking 1 : %d\n", what, max);
return max;
}
int
main(void)
{
int a, b, c, max;
printf("enter value a :");
scanf("%d", &a);
printf("enter value b :");
scanf("%d", &b);
printf("enter value c :");
scanf("%d", &c);
getmax("value",a,b,c);
int number1, number2, number3;
number1 = a;
number2 = b;
number3 = c;
//checking 2 main programs
getmax("score",number1,number2,number3);
return 0;
}
Step 6: Put user input into common function:
#include <stdio.h>
// ask user for number
int
asknum(const char *prompt)
{
char buf[1000];
int num;
printf("enter value %s : ",prompt);
fflush(stdout);
fgets(buf,sizeof(buf),stdin);
sscanf(buf,"%d",&num);
return num;
}
// checking 1 program module
int
getmax(const char *what,int a,int b,int c)
{
int max;
if ((a > b) && (a > c)) {
max = a;
}
else if (b > c) {
max = b;
}
else {
max = c;
}
printf("highest %s checking 1 : %d\n", what, max);
return max;
}
int
main(void)
{
int a, b, c, max;
a = asknum("a");
b = asknum("b");
c = asknum("c");
getmax("value",a,b,c);
int number1, number2, number3;
number1 = a;
number2 = b;
number3 = c;
//checking 2 main programs
getmax("score",number1,number2,number3);
return 0;
}
Convert program to use arrays rather than individual scalars:
#include <stdio.h>
// ask user for number
int
asknum(int prompt)
{
char buf[1000];
int num;
printf("enter value %d : ",prompt);
fflush(stdout);
fgets(buf,sizeof(buf),stdin);
sscanf(buf,"%d",&num);
return num;
}
// checking 1 program module
int
getmax(const char *what,const int *arr,int count)
{
int idx;
int val;
int max = -1;
if (count > 0) {
max = arr[0];
for (idx = 1; idx < count; ++idx) {
val = arr[idx];
if (val > max)
max = val;
}
printf("highest %s checking 1 : %d\n", what, max);
}
return max;
}
int
main(void)
{
int idx;
int count = 3;
int arr[count];
for (idx = 0; idx < count; ++idx)
arr[idx] = asknum(idx);
getmax("value",arr,count);
int number[count];
for (idx = 0; idx < count; ++idx)
number[idx] = arr[idx];
//checking 2 main programs
getmax("score",number,count);
return 0;
}
Convert program to prompt user for number of elements in the array
#include <stdio.h>
// ask user for number
int
asknum(const char *prompt)
{
char buf[1000];
int num;
printf("%s : ",prompt);
fflush(stdout);
fgets(buf,sizeof(buf),stdin);
sscanf(buf,"%d",&num);
return num;
}
// ask user for array value
int
askval(int idx)
{
char prompt[100];
int num;
sprintf(prompt,"Enter array value %d",idx);
num = asknum(prompt);
return num;
}
// checking 1 program module
int
getmax(const char *what,const int *arr,int count)
{
int idx;
int val;
int max = -1;
if (count > 0) {
max = arr[0];
for (idx = 1; idx < count; ++idx) {
val = arr[idx];
if (val > max)
max = val;
}
printf("highest %s value : %d\n", what, max);
}
return max;
}
int
main(void)
{
int idx;
int count = asknum("Enter the number of array elements");
int arr[count];
for (idx = 0; idx < count; ++idx)
arr[idx] = askval(idx);
getmax("value",arr,count);
int number[count];
for (idx = 0; idx < count; ++idx)
number[idx] = arr[idx];
//checking 2 main programs
getmax("score",number,count);
return 0;
}
I am learning about functions and how to call upon them and use them in class. I don't quite understand where I've gone wrong here. I know that there are some mistakes around the int main part. I have asked my teacher and he is reluctant on giving me an example that would solve my problems or help me out. I think my main problem is at factorial_result = factorial();
#include <stdio.h>
void mystamp(void)
{
printf("My name is John Appleseed\n");
printf("My lab time is 12:30 on Sunday\n");
return;
}
int getnum(void)
{
int local_var;
printf("Please enter an integer: ");
scanf("%d%*c", local_var);
return(local_var);
}
int factorial(void)
{
int x,f=1,local_var;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial();
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
Declare local_var as a global variable and do:
local_var = getnum();
OR
Change main() to:
int main(void)
{
int result;
int factorial_result;
mystamp();
result = getnum();
factorial_result = factorial(result);
printf("You typed %d\n", result);
printf("The factorial is %d\n", factorial_result);
return;
}
And factorial() to:
int factorial(int n)
{
int x,f=1,local_var=n;
for(x=1; x <= local_var; x++)
f = f * x;
return(f);
}
Your factorial should be calculated based on the input( i.e in your case int result ).
So, your method factorial() should looks as follows :
int factorial( int number )
{
int factorial_value = 1;
while( number > 0 )
{
factorial_value *= number;
number--;
}
return factorial_value;
}
Then, the correct factorial would be returned and printed accordingly ! Regarding the scope of the variables that you have used, see the comments under your question.
#include <stdio.h>
int factorial(int);
int main()
{
int num;
int result;
printf("Enter a number to find it's Factorial: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * factorial(num - 1));
}
}
This is a simple factorial program using recursion calling function !
include
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate its factorial\n"); scanf("%d", &n);
for (c = 1; c <= n; c++) fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
I have written a program to check for Palindrome number.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int n,i;
printf("Please enter a number: ");
scanf("%d", &n);
/* Function Prototypes */
int reverse(int *p);
i=reverse(&n);
printf("Number returned %d",i);
if (i == n)
{
printf("The number is a palindrome");
}
else
{
printf("The number is NOT a palindrome");
}
}
int reverse( int *p)
{
int rev=0;
while(*p !=0)
{
rev=rev*10;
rev=rev+ *p%10;
*p=*p/10;
}
return (rev);
}
But it's always showing "Number is not a palindrome " irrespective of number is not a palindrome or not.
The reverse function leaves its argument pointing to zero. The argument doesn't need to be a pointer, and passing n by value instead solves the problem.
Here's fixed code, somewhat reformatted and with error-checking added.
#include <stdio.h>
int reverse(int p) {
int rev = 0;
while (p != 0) {
rev = rev * 10;
rev = rev + p%10;
p = p/10;
}
return rev;
}
int main(void) {
int n, i;
printf("Please enter a number: ");
if (scanf("%d", &n) != 1) {
printf("failed to read number.\n");
return 1;
}
i = reverse(n);
if (i == n) {
printf("%d is a palindrome: reversing it gives %d\n", n, i);
} else {
printf("%d isn't a palindrome: reversing it gives %d\n", n, i);
}
return 0;
}
It's an important skill to be able to debug programs. Here's a good link for some beginner techniques: http://ericlippert.com/2014/03/05/how-to-debug-small-programs/
I in my program, I'm attempting to create two different variables that will look the user's input integers and count how many are over 20 and how many are between 10 and 90. Unfortunately, both functions (counter_20 and between_count) return the same result, and when printed, it is an exorbitantly high and incorrect number. Any help would be greatly appreciated! My code is as follows:
#include <stdio.h>
#include <stdbool.h>
int getNumber(void);
float average_counter(int sum, int numCounter);
int counter_20(int input);
int between_counter(int input);
void print_results(int sum, int average, int numCounter, int sumOfTwenties, int betweenCount);
int main (void){
int a, b, c, e, f;
float d;
bool x = true;
a = 0;
b = 0;
c = 0;
d = 0;
e = 0;
f = 0;
while(x = true){
if(a != 9999){
if(a != 0){
b += a;
c++;
d = average_counter(b, c);
e = counter_20(a);
f = between_counter(a);
}
else{
printf("No input was provided.");
}
}
else{
break;
}
}
print_results(b, d, c, e, f);
}
int getNumber(void){
int input;
printf("Please input an integer. If you would like to stop inputting integers and see the$
scanf(" %d", &input);
return input;
}
float average_counter(int sum, int numCounter){
float average;
average = sum/numCounter;
return average;
}
//One of my functions for counting the number of integers over 20
int counter_20(int input){
int countTwenties;
if(input > 20){
++countTwenties;
}
printf(" %d", countTwenties);
return countTwenties;
}
//my function for counting the integers between 10 1nd 90
int between_counter(int input){
int betweenCount;
if(input < 90){
if(input > 10){
++betweenCount;
}
else{
}
}
return betweenCount;
}
void print_results(int sum, int average, int numCounter, int countTwenties, int betweenCount){
printf("\nThe sum is: %d", sum);
printf("\nThe average is: %d", average);
printf("\nThe number of integers is: %d", numCounter);
printf("\nThe number of integers over 20 is: %d", countTwenties);
printf("\nThe number of integers between 10 and 90 is: %d", betweenCount);
return;
}
int counter_20(int input){
int countTwenties;
if(input > 20){
++countTwenties;
}
printf(" %d", countTwenties);
return countTwenties;
}
countTwenties is uninitialized and using uninitialized variables lead to undefined behavior. Fix it by initializing the variable like
int countTwenties=0;
As pointed out in my comment
while(x = true)
should be
while(x == true)
Normally compiler throws a warning when you have something like this in the code
I want to do a void factorial function in C that uses pointers. This is my code but it won't run.
#include <stdio.h>
void factorial(int, int*);
int main()
{
int n;
int r = 0;
printf("Enter n : ");
scanf("%d", &n);
if (n < 0)
{
printf("No factorial for negative");
}
else
{
factorial(n , &r);
printf("factorial of %d is %d", n, r);
}
}
void factorial(int n , int *r)
{
if (n == 0 || n == 1)
{
*r= 1;
}
else
{
*r= n* factorial((n-1), r);
}
}
The error I get is in the last line of the code saying : invalid operands of type "int" and "void" to binary operator * , what does that mean and how to fix it?
Well, if factorial() returns void (which is basically , returning nothing) , this line does not make any sense
*result = num * factorial ((num-1), result);
It's because factorial() returns void.
Try:
factorial((num-1), result);
*result= num *(*result);
Instead of that line.
You cannot use return value of a function if it is declared to return void.
If you want to declare function this way then your factorial function can be:
void factorial(int num , int *result)
{
if (num == 0 || num == 1)
{
//*result = 1;
return;
}
else
{
*result *= num;
factorial ((num-1), result);
}
But also in main function you should change int res = 0 into int res = 1.
void Factorial(int *a,int *result){
if (*a == 0 || *a == 1){
return;
}
else{
*result *= *a;
*a -= 1;
Factorial(a,result);
}}