COMPUTER SCIENCE ARRAYS

3.c) Write a function in C++ to find the sum of diagonal elements from a 2D array of type float. Use the array and its size as parameters with float as its return type.

Solution:

float diasum(float A[ ][ ],int R,int C)
{ int i,j;
float Dsum=0.0;
for(i=0;i<R;i++)
for(j=0;j<C;j++)
if((i= = j)| | (i+j)= =(size-1))
Dsum=Dsum+A[i][j];
return Dsum;
}

DELHI 2003:

3.a) Assume a array E containing elements of structure Employee is required to be arranged in descending order of Salary. Write a C++ function to arrange same with the help of bubble sort, the array and its size is required to be passed as parameters to the function. Definition of structrure Employee is as follows:

Struct Employee
{ int Eno;
char name[25];
float Salary;
};

Solution:

void bubble(Employee E[ ],int n)
{ int i,j;
Employee Etemp;
for(i=0;i<n;++i)
for(j=0;j<(n-1)-i ;j++)
if(E[j].salary<E[j+1].salary)
{ Etemp=E[j];
E[j]=E[j+1];
E[j+1]=temp;
}
cout<<"The details of the employee in ascending order of salary ";
for(i=0;i<n;i++)
cout<<E[i].Eno<<'\t'<<E[i].name<<’\t<< E[i].Salary<<endl;
}

3.b) An array X[30][10] is stored in the memory with each element requiring 4 bytes storage. Find out the Base address of X is 4500, find out memory locations of X[12][8] and X[2][14], if the content is stored along the row.

Solution: Children, Try this answer as an assignment.

3.c) Write a user-defined function in C++ to display those elements of 2D array T[4][4] which are divisible by 100. Assume the content of the array is already present and the function prototype is as follows:

void showhundred( int T[4][4]);
void showhundred(int T[4][4])
{ int i,j;
cout<<”\nThe elements in the array
which are divisible by 100 …..”;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(T[i][j]%100= =0)
cout<<T[i][j]<<’\t’;
}

 

CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )