Sheet 2 Solution C
Sheet 2 Solution C
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;
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;
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;
}
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};
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};
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];
}
}
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;
}
}
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;
char c;
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--;
}
return 0;
}
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
if (s2[i] != '\0')
return -1;
return 0;
}
return 0;
}
return len;
}
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];
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;
int occurances=0;
return 0;
}