JAHAN INSTITUTE OF HIGHER EDUCATION
Department of Computer Sciences
CSC241 Introduction to Computer
Programming - II
Assignment 1
Name:
Registration ID:
Q1: Check out the following program and
a) Remove the errors if any and write error free program.
b) Write the output of the program.
c) Also explain how the program works
Before correction
int find(int x, int y, int z)
{
int temp;
z = x + y;
temp = x;
x = y;
y = 2 * temp
printf(%d %d %d \n,x,y,z);
return y;
}
int main()
{
int a, b, c;
a = 15;
b = 25;
c = 30;
printf(%d %d %d %d \n, a,b,c,find(a,b,c,x));
}
After correction
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
find(int x, int y, int z )
{
int temp;
z = x + y ; //a+b =40
temp = x;
x = y; // b=25
y = 2 * temp ; // a * 2; = 30
printf("%d %d %d \n",x,y,z);
return 0;
}
int main()
{
int a, b, c;
a = 15; // x
b = 25; // y
c = 30; // use for noting
printf("%d %d %d \n",a,b,c,find(a,b,c));
getch();
}
Q2: Modify the below program to call by reference.
Before modifying
int maximum(float, float);
main ()
{
float a,b; intans;
a=b=2;
ans= maximum(a,b) /* returns 1 if a > b, 2 if b > a, 0 otherwise */
}
int maximum(float a, float b)
{
if ( a > b ) return 1;
if ( b > a ) return 2;
return 0;
}
After modifying
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
int maximum(float, float);
main ()
{
float a,b; intans;
a=1;
b=2;
ans = maximum(a,b); /* returns 1 if a > b, 2 if b > a, 0 otherwise */
cout<<ans;
getch();
}
int maximum(float a, float b)
{
if ( a > b ) return 1;
if ( b > a ) return 2;
return 0;
Q3: Write function prototype for each of the following description.
a) A function called funtion01 that has no parameters and returns an integer.
#include <iostream.h>
#include <conio.h>
int function01()
{
inta,b,c;
a=10;
b=20;
c=a+b;
cout<<c;
return 0;
}
void main() {
function01();
getch();
}
b) A function called function01 that has no parameters and also returns no value
#include <iostream.h>
#include <conio.h>
void function01()
{
inte,d,c;
e=10;
d=20;
c=d*e;
cout<<c;
}
void main() {
function01();
getch();
}
a) A function called ReturnInteger that has an integer parameter called Value
followed by floating point value called Number. The function also returns an
integer.
b) Function intoDouble that takes an integer number and returns a double.
Thank you!!!