0% found this document useful (0 votes)
24 views8 pages

Sheet 2 Solution C

Uploaded by

Mahmoud el-saidy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views8 pages

Sheet 2 Solution C

Uploaded by

Mahmoud el-saidy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Sheet 2 solution

1. Write a program to find the maximum and minimum numbers in a list of N integers. The program should
be able to find the location of the maximum and the minimum. Test your program on a list of 10 numbers
of your choice.
#include<iostream>
using namespace std;

int main()
{
const int N = 10;
int list[N] = {0,6,-1,7,2,9,10,3,-3,5};
int max, min, maxLoc, minLoc;

max = list[0]; maxLoc = 0;


min = list[0]; minLoc = 0;

for(int i=1;i<N;i++)
{
if(list[i] > max)
{
max = list[i];
maxLoc = i;
}
if(list[i] < min)
{
min = list[i];
minLoc = i;
}
}

cout << "Max = " << max << " at index " << maxLoc << endl;
cout << "Min = " << min << " at index " << minLoc << endl;
return 0;
}

2. Write a program that reads the values of an array called score of size N and then reverses the order of the
array (i.e. score[0] goes into score[N-1], score[1] goes into score[N-2], and so on). You must do the
operation without an external array (i.e. the change of order happens in the same array). Discuss the case
of even and odd values of N.
#include<iostream>
using namespace std;

void swap (int & x,int & y)


{
int temp;
temp = x;
x = y;
y = temp;
}

int main()
{
const int N = 5;
int score[N];
for(int i=0; i < N; i++)
{
cout << "Enter element number: ";
cin >> score[i];
}

int temp;
for(int i = 0; i < N/2; i++)
{
swap(score[i],score[N-1-i]);
// or:
//temp = score[i];
//score[i] = score[N-1-i];
//score[N-1-i] = temp;
}

for(int i = 0; i < N; i++)


cout << score[i] << " ";

return 0;
}

3. Write a program to read the marks of 10 students in 4 different courses. The program is required to
display the total score and the grade of each student and the average mark obtained by the students in
each course.

#include<iostream>
using namespace std;

int main()
{
const int num_students = 10;
const int num_courses = 4;

float grades[num_students][num_courses];
float student_total[num_students]={0}; // this initializes all elements to 0
float course_average[num_courses]={0};

for(int i = 0; i < num_students; i++)


{
for(int j = 0; j < num_courses; j++)
{
cout << "Enter the grade of student " << i+1 << " in course " << j+1 << ": ";
cin >> grades[i][j];
}
}

for(int i = 0; i < num_students; i++)


{
for(int j = 0; j < num_courses; j++)
{
student_total[i]+=grades[i][j];
course_average[j]+=grades[i][j]/num_students;
}
}

for(int i = 0; i < num_students; i++)


{
for(int j = 0; j < num_courses; j++)
{
cout << "Grade of student " << i+1 << " in course " << j+1 << ": " <<
grades[i][j] << endl;
}
cout << "Total score of student " << i+1 << ": " << student_total[i] << endl;
}
for(int j = 0; j < num_courses; j++)
{
cout << "Average grade of course " << j+1 << ": " << course_average[j] << endl;
}

return 0;
}

4. Write a program to multiply two matrices. The program should read the dimensions of the two
matrices and test if the multiplication is possible and then read the elements of each matrix and then
print the resulting matrix.
#include<iostream>
using namespace std;

int main()
{
const int max_dim=100; //maximum dimension of any matrix
float A[max_dim][max_dim]={0};
float B[max_dim][max_dim]={0};

int A_rows, A_cols, B_rows, B_cols;

cout << "Enter the number of rows of A: " ;


cin >> A_rows;
cout << "Enter the number of columns of A: " ;
cin >> A_cols;

cout << "Enter the number of rows of B: " ;


cin >> B_rows;
cout << "Enter the number of columns of B: " ;
cin >> B_cols;

if(A_cols != B_rows)
{
cout << "Matrix dimensions mismatch!\n";
return 0;
}

cout << "Enter the elements of the first matrix:" << endl;
for (int i = 0; i < A_rows; i++)
for (int j = 0; j < A_cols; j++)
{
cout << "Enter the element A[" << i << "][" << j << "]: ";
cin >> A[i][j];
}

cout << "Enter the elements of the second matrix:" << endl;
for (int i = 0; i < B_rows; i++)
for (int j = 0; j < B_cols; j++)
{
cout << "Enter the element B[" << i << "][" << j << "]: ";
cin >> B[i][j];
}

// Product
float C[max_dim][max_dim]={0};
for(int i = 0; i< A_rows;i++)
{
for(int j=0;j<B_cols; j++)
{
for(int k=0; k<A_cols; k++)
C[i][j] += A[i][k] * B[k][j];
}
}

cout << "Product matrix: " << endl;


for(int i = 0; i< A_rows;i++)
{
for(int j=0;j<B_cols; j++)
{
cout << C[i][j] << "\t"; // \t is tab, you may use spaces " " instead of "\t" or
setw()
}
cout<<endl;
}
return 0;
}

5. Write a program to sort a list of N integers into an ascending order. Test your program on the list you
defined in problem 1.
#include<iostream>
using namespace std;

int main()
{
const int N = 10;
int list[N] = {0, 6, -1, 7, 2, 9, 10, 3, -3, 5};

int temp;
for(int i = 0; i < N-1; i++)
for(int j = i+1; j < N; j++)
{
if(list[i] > list[j])
{ // or use swap()
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}

for(int i = 0; i < N; i++)


cout << list[i] << endl;

return 0;
}
6. Write a program that reads a single character from ‘A’ to ‘Z’ and produces a pyramid of letters. For
example, if the input is ‘E’, the output is :

A
ABA
ABCBA
ABCDCBA
ABCDEDCBA

#include<iostream>
using namespace std;

int main()
{
char ch;
cout << "Enter a character: ";
cin >> ch;

int leading_spaces = ch - 'A';


int num_lines = ch - 'A' + 1;

char c;

for (int line = 1; line <= num_lines; line++)


{
for (int k = 0; k < leading_spaces; k++) cout << " ";
leading_spaces--;
c = 'A';

for (int j = 1; j < line; j++)


cout << c++; // prints c, then increments it, i.e. get the next alphabetic
character
for (int j = 0; j < line; j++)
cout << c--; // prints c, then decrements it, i.e. get the previous alphabetic
character
cout << endl;
}

return 0;
}

8. Write a program to convert a decimal integer n to a binary integer. The conversion can be
implemented by dividing n by 2 and repeat the division for the quotient until the quotient is zero. The
remainders of all the divisions represent the resulting binary number.

#include<iostream>
using namespace std;

int main()
{
int num;
cout << "Enter a decimal integer: ";
cin >> num;

int bin[100]={0};
int len=0, rem;

while(num)
{
rem = num % 2; // mod [ this must be first]
num = num / 2; // integer division
bin[len++] = rem; // or bin[len]=rem; len--;
}

cout<< "Binary equivalent = ";


for(int i = len - 1; i >= 0; i--)
cout << bin[i];

return 0;
}

9. Write the code for the following string manipulation functions:

a. void StrToUpper(char str[ ]) that converts a given string to uppercase. Use the function char
toupper(char ch) that returns the uppercase of the argument ch.
b. int strcmp(char s1[ ], char s2[ ]) as described in lecture
c. void strcat(char s1[], char s2[]) as described in lecture
d. int strlen(char s1[])as described in lecture

void StrToUpper(char str[])


{
for(int i=0; str[i]; i++) // or str[i] != '\0', or str[i] != 0
str[i] = toupper(str[i]);
// or for(int i=0; i < strlen(str); i++)
}

int strcmp(char s1[], char s2[])


{
int i = 0;
while (s1[i] != '\0')
{
if ((s2[i] == '\0') || (s1[i] > s2[i]))
return 1;
else if (s1[i] < s2[i])
return -1;
i++;
}

if (s2[i] != '\0')
return -1;
return 0;
}

int strcmp2(char s1[], char s2[])


{
int i;
for(i=0; s1[i] && s2[i];i++)
{
if(s1[i] > s2[i])
return 1;
else if(s1[i] < s2[i])
return -1;
}

if(s1[i] != 0 && s2[i] == 0) return 1;


if(s1[i] == 0 && s2[i] != 0) return -1;

return 0;
}

Try to write the code for strcmpi(), which is case insensitive


void strcat(char s1[], char s2[])
{
int len1 = strlen(s1);
int i; // we want to use i after the loop
for (i = 0; s2[i]; i++)
s1[len1 + i] = s2[i];
s1[len1 + i] = '\0';
}

int strlen(char s1[])


{
int len = 0;

while(s1[len]) // or for (len = 0; s1[len]; len++);


len++;

return len;
}

We may write the code for toupper:


char toupper(char c)
{
if(c >= 'a' && c <= 'z')
return c - ('a' - 'A'); //ASCII of 'a' is 97 and 'A' is 65
}

11. Write a C++ program that reads the names of 100 students as an array of strings then search for
certain name in the array.
#include<iostream>
using namespace std;

int main()
{
const int num_students = 5;
const int max_length = 100;
char target[max_length];
char names[num_students][max_length];

cout << "Enter names: ";


for (int i=0; i<num_students; i++)
cin.getline(names[i], max_length); // cin >> names[i];

cout << "Enter target: ";


cin.getline(target, max_length);
// cin >> target; for one word only

for(int i=0; i<num_students; i++)


if(strcmp(names[i],target) == 0)
{
cout<<"Found at " << i << endl;
}

return 0;
}
12. Write a C++ program that reads any string and a character, then count the number of occurrence of
the character in the string.

#include<iostream>
using namespace std;

int main()
{
const int max_length = 100;
char str[max_length];
char target;

cout << "Enter a string: ";


cin >> str; // or cin.getline(str,100);

cout << "Enter a character to search for: ";


cin >> target;

int occurances=0;

for (int i=0; str[i]; i++) // or str[i]!=’\0’


if(str[i] == target)
occurances++;

cout << occurances << " occurances" << endl;

return 0;
}

You might also like