Complete solution of mid term paper...
#include<conio.h>
#include<stdio.h>
/*
Write a program that can take 2 float and 1 character values as input.
Define four functions add, sub, mult and div for addition, subtraction,
multiplication and division respectively. Define a procedure by using
switch statement in which the functions are called by analyzing the
character input i.e. '+' for addition, '-'for subtraction, '*' for
multiplication and '/' for division. The functions will then return the
result for printing to the main function. */
float div(float,float);
float mult(float,float);
float sub(float,float);
float add(float,float);
main()
{
float num1=1.0,num2=1.0, result=0.0;
char op;
while(!(num1==0.0 && num2==0.0))
{
printf("Enter number,operator and number\n");
scanf("%f %c %f", &num1,&op,&num2);
switch(op)
{
case '+':
result = add(num1,num2);
break;
case '-':
result = sub(num1,num2);
break;
case '*':
result = mult(num1,num2);
break;
case '/':
result = div(num1,num2);
break;
default:
printf("Invalid operator");
break;
}
printf("The result is %.2f\n", result);
}
getch();
}
float add(float num1, float num2)
{
float result=0.0;
result =num1+num2;
return (result);
}
float sub(float num1,float num2)
{
float result=0.0;
result = num1-num2;
return (result);
}
float mult(float num1,float num2)
{
{
b)
#include<conio.h>
#include<stdio.h>
/*Write
a program that asks the user to enter the total rainfall for each of 12
months into a one dimensional array of type float. Then
main function calls min and max functions and pass the array as
parameter; min and max will return the highest and lowest amount of
rainfall to the main function. Do not accept negative numbers*/
#define LIM 12
float min(float[],int);
float max(float[],int);
main()
{
float array[LIM];
int index=0;
float lowest=0.0, highest=0.0;
do
{
if(index>=LIM)
{
printf("Buffer full.\n");
index++;
break;
}
printf("Enter rainfall for %d months: ", index);
scanf("%f",&array[index]);
}while(array[index++]>0);
lowest= min(array,index-1);
highest= max(array,index-1);
printf("\nLowest rainfall of the year: %0.2f\n", lowest);
printf("\nHighest rainfall of the year: %0.2f\n",highest);
getch();
}
float min(float array[], int index)
{
float mindex=0.0;
int i;
mindex= array[0]; //assume
for(i=1;i<index;i++)
{
if(mindex>array[i])
mindex=array[i];
}
return(mindex);
}
//function for finding max value.
float max(float array[], int index)
{
float maxdex=0.0;
int i;
maxdex= array[0]; //assume
for(i=1;i<index;i++)
{
if(maxdex<array[i])
maxdex=array[i];
}
return(maxdex);
}
Q1.(c)
#include<conio.h>
#include<stdio.h>
// Add two 3 x3 matrices.
main(){
int m=0,n=0,c,d;
int firstmatrix[3][3], secondmatrix[3][3], sum[3][3];
printf("Enter numbers for matrix 1\n"); //input for 1st matrix
for(c=0;c<3;c++)
for(d=0;d<3;d++)
scanf("%d",&firstmatrix[c][d]) ;
printf("Enter numbers for matrix 2\n"); //input for second matrix
for(c=0;c<3;c++)
for(d=0;d<3;d++)
scanf("%d",&secondmatrix[c][d] );
for(c=0;c<3;c++) //Adding both matrices
for(d=0;d<3;d++)
sum[c][d] = firstmatrix[c][d]+ secondmatrix[c][d];
printf("Sum of entered matrices are \n");
for(c=0;c<3;c++)
{
for(d=0;d<3;d++)
printf("%d\t",sum[c][d]);
printf("\n");
}
getch();
}
d)
#include<conio.h>
#include<stdio.h>
//Exchange names using 2D array of characters
#define MAX 20
main(){
static char name[][MAX] = {
"Paul",
"Sam",
"John",
"Peter",
"Joe"
int i;
char t;
for(i=0;i<5;i++)
{
printf("\n %s",name[i]);
}
for(i=0;i<MAX;i++)
{
t = name[2][i];
name[2][i] = name[3][i];
name[3][i] = t;
}
printf(“\n\n Newlist\n”);
for(i=0;i<5;i++)
{
printf("\n%s", name[i]);
}
getch();
}
Q2.(a)
#include<conio.h>
#include<stdio.h>
#define PRODUCT(y) (y * y)
main()
{
int i=3,j;
j = PRODUCT(i+1);
printf("\n%d", j);
getch();
}
/*output = 7
Reason:
i=3;
macro will expand in this form:
j = PRODUCT(i+1);
j= (3+1*3+1);
j= 3+3+1
j = 7 */
b) #include<conio.h>
#include<stdio.h>
main()
{
int max=5,i;
float arr[max];
printf("Enter numbers");
for(i=max;i>0;i--)
scanf("%f", &arr[i]);
getch();
}
Output:
5
4
3
2
1
Q3.
· Scanf() and gets()
The scanf() function, reads input for numbers and other datatypes from standard input.
The gets() function reads characters from stdin and stores them as a string into str until a newline character ('\n') or the End-of-File is reached. The ending newline character ('\n') is not included in the string. A null character ('\0') is automatically appended after the last character copied to str to signal the end of the C string.
· Printf() and puts()
The printf() function writes to the standard output. Printf() could be thought of as a more powerful version of puts(). `printf' provides the ability to format variables for output using format specifiers such as %s, %d, %lf, etc...
The puts() function only permits you to print single string on the output screen.
· Break and continue
The continue statement provides a convenient way to force an immediate jump to the loop control statement. The break statement terminates the execution of the loop.
· Function definition and function declaration
The prototype statement for a function declares it, i.e, tells the compiler about the function name, return type, and number and type of its parameters.
The function header, followed by the body of the function, defines the function giving the details of the steps to perform the function operation.
· Scanf() and gets()
The scanf() function, reads input for numbers and other datatypes from standard input.
The gets() function reads characters from stdin and stores them as a string into str until a newline character ('\n') or the End-of-File is reached. The ending newline character ('\n') is not included in the string. A null character ('\0') is automatically appended after the last character copied to str to signal the end of the C string.
· Printf() and puts()
The printf() function writes to the standard output. Printf() could be thought of as a more powerful version of puts(). `printf' provides the ability to format variables for output using format specifiers such as %s, %d, %lf, etc...
The puts() function only permits you to print single string on the output screen.
· Break and continue
The continue statement provides a convenient way to force an immediate jump to the loop control statement. The break statement terminates the execution of the loop.
· Function definition and function declaration
The prototype statement for a function declares it, i.e, tells the compiler about the function name, return type, and number and type of its parameters.
The function header, followed by the body of the function, defines the function giving the details of the steps to perform the function operation.