Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Computer System & Programming

(Solutions)
1. a ) ex:-
#include <stdio.h> [1]
main()
{
int a=1,b=4;
printf(“press \n 1 for addition\n 2 for substraction 3 for multiplication 4 for multiplication”);
switch (a)
{
case 1: printf(“%d”,a+b);break;
case 2: printf(“%d”,a-b);break;
case 3: printf(“%d”,a/b);break;
case 4: printf(“%d”,a*b);break;
default: printf(“You have entered the wron option”);
}
}
In this program we have multiple options in those cases we use switch statement in this you can see
(a)case options must be of integer values and variables are not accepted, otherwise it will show errors.
(b)And if all cases fails then it will go to the default
(c) break statement is necessary otherwise if one case get succeeded other cases which follow after it
also executed. [1]

1. b) Array is a contiguous memory allocation of same data type, and if that data type is of integers then
we can say it is a integer array. [1]
Limitations: [1]
(1) In some case at the run time we can know what is the length array needed, such a time at
the declaration time we have to declare sufficiently large array for it so there is possibility of
memory wastage.
(2) If you want to insert new element in between array then that is very tedious process.

1. c) ex:- struct student


{
Char name[20];
int marks;
}students[20];
char students_names[20][50];
int students_marks[20]; [1]
Array is only for one type data, you cannot store multiple type of data within single array. In the above

CSP_Solutions Page 1
you can see we have defined one structure and after that declared 20 instances of that structure
declared. But if it is in array we have to define two arrays. [1]

1. d) prototype of malloc function is void * malloc(int); [1]


So this will be returning void pointers that we have to type caste to type data you want to allocate
memory and it will be taking size in byte , how much memory it will be required.
int * p;
p= (int *)malloc(10*sizeof(int)); [1]

1. e)
1) int a=0; [1]
while(a==0)
{
printf(“a=0”);
}
2) int a=-1;
while(a<0)
{
a=a-1; [1]
}

1. f) in this output:- Kiit in [1]


Justification:- for(i=0;i<=5;,i++); is written so for loop will be ended . printf statement won’t be coming
into for loop so it has printed only once.

1. g) Output :- 16 , 14, 15, 20 (or) 16,15,15,20 [1]


Justification:- post increment and pre increment [1]
(or)
Error because pf(“ ”); there is no such library function in stdio.h file [2]

1. h) Output : error [1]


Justification: Because there is no library function such as pf(“”) in stdio.h [1]
(Or)
Output: 10 7 3 1 [1]
Justification: recursion [1]

1. i) in this problem take A=10 and B=1200 then


A=A+1 means A will have value of 11 [1]
B=B+1 means B will be having value of 1202 (Depending on compiler as size of int varies) [1]

1 . j) Output: Error [1]

CSP_Solutions Page 2
Justification:- as pf() there is no library function for that. [1]
2. a) main()
{
static int arr[26]; [1]
int i=0,p=0;
char name[20];
printf("Enter the string\n");
scanf("%s",name);
while(name[i]!='\0') [1]
{
arr[name[i]-96]++; [1]
i++;
}
for(i=0;i<26;i++) [1]
{
if((arr[i]%2)!=0)
p++;
}
if(p>1) [1]
printf("\nIt cannot form a pallindrom");
else
printf("\nIt can form a pallindrom");

2. b)
Break will be useful in breaking out of loops and switch statements [1]
When loop is running in between completion of all iterations of loop if you want come out then
loop will be used. In loops if else statements will be associated with break statements [1]
In switch statements if one case gets succeeded and don’t want to execute other case statements then
we will write break statement [13)

3) struct student [2]


{
int roll_n0;
char name[20],branch[10];
};
main()
{
int i=0;
struct student student_record[5]; [1]
for (i=0;i<5;i++)

CSP_Solutions Page 3
student_record[i].name=”IT”; [1]

for(i=0;i<5;i++)
{
if(student_record[i].name[0]==’a’|| student_record[i].name[0]==’e’||
student_record[i].name[0]==’i’|| student_record[i].name[0]==’o’||
student_record[i].name[0]==’u’) [2]
printf(“\nName=%s\nBranch=%s\nRoll number=%d”, student_record[i].name, [2]
student_record[i].branch, student_record[i].roll_no);
}
}

4)
(i) main()
{
int arr[5],i,k,temp;
for(i=0;i<5;i++)
{ printf(“Enter the value of arr[%d]”,i);
scanf(“%d”,&arr[i]); [1]
}
for(i=0;i<5;i++)
{
if(arr[i]%2==0)
continue; [1]
for(j=0;j<5;j++)
{ if(arr[j]%2 ==0)
continue; [2]
if(arr[i]>arr[j])
{
temp= arr[i]; [2]
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}

4. (ii) main()
{ int arr[5],i,sum=0;
for(i=0;i<5;i++)
{ printf(“Enter the value of arr[%d]”,i);

CSP_Solutions Page 4
scanf(“%d”,&arr[i]); [1]
}
for(i=0;i<5;i++)
{
if(arr[i]%2==0)
sum=sum+arr[i];
}
printf(“sum of even numbers is=%d”,sum);
}
5)

#include<stdio.h>
#include<string.h>
main()

{
int cond[5]={}; //this array stores which name follows the condition of having ‘u’
after ‘a’

int count_vowels[5]={}; //this array stores the number on vowels in each name [1]
int index[5]={0,1,2,3,4}; //this array stores the index of the name
char name[5][50]; //this array stores the names
int i,j,k; //scanning the names
for(i=0;i<5;i++) [1]
{
scanf("%s",name[i]);
}
//the following part is to count the vowels and check for the condition
for(i=0;i<5;i++) [1]
{
k=strlen(name[i]);
for(j=0;j<k;j++)
{
if(name[i][j]=='a' || name[i][j]=='e' || name[i][j]=='i' || name[i][j]=='o' ||
name[i][j]=='u') [1]
(count_vowels[i])++;
if((j+1)<k)
if(name[i][j]=='a' && name[i][j+1]=='u') [1]
cond[i]=1;
}
}

CSP_Solutions Page 5
//the following part is to sort in ascending order
int temp,temp1;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(count_vowels[i]>count_vowels[j]) [1]
{
temp=count_vowels[i];
count_vowels[i]=count_vowels[j];
count_vowels[j]=temp;
temp1=index[i];
index[i]=index[j];
index[j]=temp1;
}
}
}
//the following part prints the name in ascending order of number of vowels
printf("ascending order part\n");
for(i=0;i<5;i++)
{
printf("%s\n",name[index[i]]); [1]
}
//the following part will prints the names which contains 'u' after 'a'
printf("U after A\n");
for(i=0;i<5;i++) [1]
{
if(cond[i]==1)
printf("%s\n",name[i]);
}

}
6)
#include<stdio.h>
main()
{
int a[3][5];//this 2-D array stores the numbers
int i,j; //scanning part
for(i=0;i<3;i++)
for(j=0;j<5;j++)
scanf("%d",&a[i][j]);

CSP_Solutions Page 6
int *p=&a[0][0]; //sorting part [2]
int temp;
for(i=0;i<15;i++)
for(j=i+1;j<15;j++) [1]
{
if(*(p+i)>*(p+j)) [2]
{
temp=*(p+i);
*(p+i)=*(p+j);
*(p+j)=temp;
}
}

//printing the numbers in sorted order


for(i=0;i<3;i++)
{
for(j=0;j<5;j++)
printf("%d ",a[i][j]); [1]
printf("\n");
}
//printing the diagonal elements
for(i=0;i<3;i++)
printf("%d ",a[i][i]); [2]
}

7)
#include<stdio.h>
#include<conio.h>
void results(struct student *); [1]
struct student [1]
{
int roll_no,total_marks;
char branch[20],name[25];
float attendance;
};
main()
{
struct student student_record[5]; [1]

}
void results (struct student *p) [2]
{

CSP_Solutions Page 7
int i,succeded_students; [1]
for(i=0;i<5;i++)
{
if(((p->total_marks)>=400)&&((p->attendance)>=75)) [1]
succeded_students++; [1]
}
printf(“The no of students succeded=%d”,succeded_students++);
}
8)
i) Static Storage Class: [2]

The Value of S tatic Variables persists until the end of the program. The Scope of Internal
Static Variables extend upto the end of the function in which they are defined. Internal Static Variables
can be used to retain values between function calls.

An Example....................with the static variable

ii) Dynamic Memory Allocation: [2]

The Process of Allocating Memory at run time is known as Dynamic Memory


Allocation. Memory management functions can be used to for allocating and frreing memory during
program execution.

Some Memory Allocation Functions: Malloc( ), Calloc( ), free, Realloc( ).....

iii) Call By Value And Call By Reference: [2]

The Process of calling a function using pointers to pass the addresses of variables is
known as Call By Reference. The process of passing the actual value of variables is known as Call By
Value.

An example program with necessary functions involving both the concepts(Call By Reference and Call By
Value).

iv) I/O Statements: [2]

Scanf is the function which is used to read(Input) the data and the general form is:
scanf("Control String", arg1, arg2,....,argn);

printf is the function which is used to write(Output) the data and the general form is:
printf("Control String", arg1, arg2,.....,argn);

v) Memory: [2]
The Input supplied by the input devices first goes to memory.After the data has been
processed in ALU the result is stored back in the Memory. Memory of the Computer is of two types

CSP_Solutions Page 8
Primary Memory: -Faster in speed, less in size and costlier. It consists of ROM(Read only Memory) and
RAM(Random Access Memory) . ROM is a very small amount of memory used for booting. RAM contains
all types of intermediate and temporary data to be used by the CPU.
Secondary Memory: usually very large amount of memory which is comparatively cheaper and slower
than primary memory but is permanent in nature.

CSP_Solutions Page 9

You might also like