/ Asian

# Structured Programming Solution – Asian 2nd Term

Group ‘A’

1. Why is it incorrect to declare: char *character=‘a’?

Ans: Pointer operator ‘*’ called ‘value at address’ operator whereas in the above operation pointer has stored value hence, it is wrong.

1. What is the difference between types float and double?

Ans: Following is the difference between float and double:

 float double It is used mostly in graphic libraries because very high demands for processing powers, also used situations that can endure rounding errors. Double Types are probably the most normally used data type for real values, except handling money. float is a single-precision (32 bit) floating-point data type double is a double-precision (64 bit) floating-point data type

1. What is the purpose of / and % operators? Explain with an example.

Ans:  ‘/’ operator gives output of quotient value eg : 5/2=2 and ‘%’ operator gives output of reminder value eg : 5%2=1.

1. Difference between gets() and scanf() with suitable example.

Ans: The difference between gets() and scanf()

 gets() scanf() gets will only get character string data. scanf can read multiple values from different data types. gets function takes the name of the variable to store the received value. Eg: gets (name); scanf takes the format string and list of addresses variables. Eg:scanf(“%d”,&number);

1. Define pointer variable with example.

Ans: A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address. Eg: *var-name;

1. How the array is passed in a function? Explain with an example.

Ans: To pass an entire array to a function, only the name of the array is passed as an argument. However, notice the use of [] after argument name in float average(float age[]) .eg:

#include <stdio.h>

void display(int age)

{

printf(“%d”, age);

}

int main()

{

int ageArray[] = {2, 3, 4};

display(ageArray); //Passing array element ageArray

return 0;

}

1. Differentiate among automatic, static and external storage class

Ans: Following is differentiate among automatic, static and external storage class

 Automatic Static External It is a default storage class. It is a local variable which is capable of returning a value even when control is transferred to the function call. It is a global variable.

1. What is the output of the following program?

main()

{

int a=5;

printf(“%d %d %d “,a,++a,a++,a++);

}

Ans: 8 8 6

1. What is a nested structure? Explain with example.

Ans: Nested structure in C is nothing but the structure within a structure. One structure can be declared inside other structure as we declare structure members inside a structure.

1. Explain the nested if-else statement and switch statement with suitable example.

Ans: When a series of decisions are involved, we may have to use more than one if… else statement in nested form as shown below:

if(test condition)

{

if(test condition-2)

{

Statement;

}else{

Statement1;

}

}else{

Statement3;

Statement4;

}

C program has built-in multiway decision statement known as a switch.

Syntax:

switch(expression)

{

case value1:

block1;

break;

case value2:

block-2;

break;

default :

default-block;

break;

}

Group ‘B’

1. Write a program to transpose the m*n matrix and show the input matrix and its transpose.

Ans:

#include<stdio.h>

#define m 5

#define n 4

main()

{

int a[m][n],b[m][n];

int i,j;

for(i=0;i<=m;i++)

{

for(j=0;j<=n;j++)

{

printf(“Enter values for %drow and %d coloumn :”,i,j);

scanf(“%d”,&a[i][j]);

}

}

for(i=0;i<=m;i++)

{

for(j=0;j<=n;j++)

{

b[j][i]=a[i][j];

}

}

for(i=0;i<=n;i++)

{

for(j=0;j<=m;j++)

{

printf(“%d\t”,b[i][j]);

}

printf(“\n”);

}

}

1. Write a program to read numbers in the array then find the largest number of the array using a pointer.

Ans:

#include<stdio.h>

#include<conio.h>

void main()

{

int a,i,l,*p;

p=a;

for(i=0;i<=5;i++)

{

printf(“Enter any number”);

scanf(“%d”,p);

p++;

}

p=a;

l=*p;

p++;

for(i=1;i<=5;i++)

{

if(*p>l)

{

l=*p;

}

p++;

}

printf(“largest number present in array is %d”,*p);

getch();

}

1. Write a program to read a line of text then convert all vowels characters into ‘\$’.

Ans :

#include<stdio.h>

#include<string.h>

#define n 50

void main()

{

char name[n];

int i;

gets(name);

for(i=0;i<n;i++)

{

if(islower(name[i]))

{

name[i]=toupper(name[i]);

if(name[i]==’A’||name[i]==’E’||name[i]==’I’||name[i]==’O’||name[i]==’U’)

{

name[i]=’\$’;

}

}

}

printf(“%s”,name);

getch();

}

1. Write a program to create a structure called Table in which data members are color, price & size. Create an array of the table. Read the values from keyboard & print it on the monitor.

Ans :

#include<stdio.h>

#include<string.h>

#define n 5

struct table{

int price;

float size;

char color[n];

}t[n];

void main()

{

int i;

printf(“Enter “);

for(i=0;i<n;i++)

{

printf(“Enter Price size & color of table %d”,i+1);

scanf(“%d %f %s”,&t[i].price,&t[i].size,t[i].color);

}

for(i=0;i<n;i++)

{

printf(“table%d\nPrice=%d\tsize=%f\tcolor=%s\n\n\t”,i+1,t[i].price,t[i].size,t[i].color);

}

getch();

}

1. Write a program to read a list of college name. Then print 3rd character of all the college name.

Ans :

#include<stdio.h>

main()

{

char cname;

int i;

for(i=0;i<5;i++)

{

printf(“Enter %d college name.”,i+1);

gets(cname[i]);

}

for(i=0;i<5;i++)

{

printf(“\tThird character of %d college name = %c \n\t”,i,cname[i]);

}

}

Group ‘C’

1. Write a program to read numbers in the array then perform following using switch statements.

Print only even numbers of an array

Print only odd numbers of an array

Increase all elements of the array by x

Ans :

#include<stdio.h>

#define n 10

main()

{

int num[n];

int c,i,x;

for(i=0;i<n;i++)

{

scanf(“%d”,&num[i]);

}

printf(“Enter your choice from 1 , 2 & 3”);

scanf(“%d”,&c);

switch(c)

{

case 1 :

printf(“printing even number : “);

for(i=0;i<n;i++)

{

if(num[i]%2==0)

{

printf(“%d\n”,num[i]);

}

}

break;

case 2 :

printf(“printing odd number : “);

for(i=0;i<n;i++)

{

if(num[i]%2!=0)

{

printf(“%d\n”,num[i]);

}

}

break;

case 3 :

printf(“Enter value for x”);

scanf(“%d”,&x);

printf(“printing number by increasing it by x: “);

for(i=0;i<n;i++)

{

num[i]=num[i]+x;

printf(“%d\n”,num[i]);

}

break;

}

}

1. Write a program that reads numbers in a two-dimensional array. Then calculate & display the row and column sums as well grand total.

Ans :

#include<stdio.h>

#define n 5

#define m 4

main()

{

int a[n][m],r=0,c=0,gt=0;

int i,j;

for(i=0;i<n;i++)

{

for(j=0;j<m;j++)

{

printf(“Enter elements for %d row %d colomn”,i,j);

scanf(“%d”,&a[i][j]);

}

}

for(i=0;i<n;i++)

{

for(j=0;j<m;j++)

{

r=r+a[i][j];

gt=gt+a[i][j];

}

printf(“sum of %dth row :%d\n”,i,r);

r=0;

}

for(i=0;i<m;i++)

{

for(j=0;j<n;j++)

{

c=c+a[j][i];

}

printf(“sum of %dth column :%d\n”,i,c);

c=0;

}

printf(“Grand total sum = %d”,gt);

}

1. Write a program to multiply two matrices. For this creates three functions read(),multiply(),display() functions for reading, multiplying and printing resultant matrix respectively.

Ans :

#include<stdio.h>

#define n 3

void mult(int a[n][n],int b[n][n],int c[n][n]);

void print(int c[n][n]);

main()

{

int a[n][n],b[n][n],c[n][n];

mult(a,b,c);

print(c);

}

{

int i,j;

for(i=0;i<n;i++)

{

for(j=0;j<n;j++)

{

printf(“Enter values for %d row and %d column”,i+1,j+1);

scanf(“%d”,&a[i][j]);

}

}

}

void mult(int a[n][n],int b[n][n],int c[n][n])

{

int i,j,k;

for(i=0;i<n;i++)

{

for(j=0;j<n;j++)

{

c[i][j]=0;

for(k=0;k<n;k++)

{

c[i][j]+=a[i][k]*b[k][j];

}

}

}

}

void print(int c[n][n])

{

int i,j;

for(i=0;i<n;i++)

{

for(j=0;j<n;j++)

{

printf(“%d\t”,c[i][j]);

}

printf(“\n”);

}

} 