for explanation
Friday, February 19, 2010
Thursday, February 4, 2010
CSC202: Exercise on Functions 2
1. Create the following function to randomly generate numbers from
1 to 100:
generateNumber().
The function will return an integer from 1 to 100.
The program will then ask you to guess a number.
If the number is equal to the generated number, it will print message:
"Congratulations"
If the number is less than the generated number will print message:
"Too Low. Please guess again another number."
If the number is greater than the generated number it will print message:
"Too High. Please guess another number."
The objective is to use as few guesses as possible to get the right number.
Hint:
use: #include "stdlib.h" and rand() function
For example:
#include "stdlib.h"
#include "time.h"
void main()
{
srand(time(NULL));
int n = 1 + rand( )%10 //will generate number
//from 1 to 10
}
SOLUTION:
TEXT VERSION:
Add me (Healer Omega) to Facebook
1 to 100:
generateNumber().
The function will return an integer from 1 to 100.
The program will then ask you to guess a number.
If the number is equal to the generated number, it will print message:
"Congratulations"
If the number is less than the generated number will print message:
"Too Low. Please guess again another number."
If the number is greater than the generated number it will print message:
"Too High. Please guess another number."
The objective is to use as few guesses as possible to get the right number.
Hint:
use: #include "stdlib.h" and rand() function
For example:
#include "stdlib.h"
#include "time.h"
void main()
{
srand(time(NULL));
int n = 1 + rand( )%10 //will generate number
//from 1 to 10
}
SOLUTION:
TEXT VERSION:
Add me (Healer Omega) to Facebook
Wednesday, February 3, 2010
CSC202 : Exercise on Functions
1. Write a program to input two numbers and print the sum,
product and difference of the two numbers.
2. Rewrite your program in Q. 1 above in modular form.
Solution for Q. 2:
#include "stdio.h"
//---- Function Prototypes -----
int add(int s, int t);
int multiply(int s, int t);
int diff(int s, int t);
void main()
{
int a, b, sum, product, difference;
printf("enter 2 integers: ");
scanf("%d%d",&a,&b);
sum = add(a, b);
product = multiply(a,b);
difference = diff(a,b);
printf("sum = %d\nproduct= %d\ndifference = %d\n",sum,product,difference);
}
//----- Function Definitions -----
int add(int s, int t)
{
int total;
total = s + t;
return total;
}
int multiply(int s, int t)
{
return s*t;
}
int diff(int s, int t)
{
return s - t;
}
product and difference of the two numbers.
2. Rewrite your program in Q. 1 above in modular form.
Solution for Q. 2:
#include "stdio.h"
//---- Function Prototypes -----
int add(int s, int t);
int multiply(int s, int t);
int diff(int s, int t);
void main()
{
int a, b, sum, product, difference;
printf("enter 2 integers: ");
scanf("%d%d",&a,&b);
sum = add(a, b);
product = multiply(a,b);
difference = diff(a,b);
printf("sum = %d\nproduct= %d\ndifference = %d\n",sum,product,difference);
}
//----- Function Definitions -----
int add(int s, int t)
{
int total;
total = s + t;
return total;
}
int multiply(int s, int t)
{
return s*t;
}
int diff(int s, int t)
{
return s - t;
}
Subscribe to:
Posts (Atom)