0% found this document useful (0 votes)
67 views7 pages

Homework Chapter 5

The document provides homework assignments on C++ programming concepts: 1. A program to print a histogram from an array of random numbers. 2. A program that treats a character array as a string. 3. A program that passes arrays and individual elements to functions. 4. A program that reads two matrices, performs addition and multiplication, and prints the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views7 pages

Homework Chapter 5

The document provides homework assignments on C++ programming concepts: 1. A program to print a histogram from an array of random numbers. 2. A program that treats a character array as a string. 3. A program that passes arrays and individual elements to functions. 4. A program that reads two matrices, performs addition and multiplication, and prints the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

‫بسم هللا الرحمن الرحيم‬

‫‪Homework : Chapter 5‬‬

‫االسم ‪ /‬عبدالعزيز ايوب الزمام‬


‫الرقم الجامعي ‪202102084 /‬‬
1. Write C++ program for Histogram printing program
using Array

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
int main() {
srand((unsigned) time(0));
int initializeArray[100],i,frequency[10]={0},counter,j;
for (i = 0; i < 100; i++){
initializeArray[i] = (rand() % 10) + 1; //fill the array
randome number between 1 to 10 inclusive
}
for(i=1;i<=10;i++){ //find the frequency of number from 1
to 10
counter = 0;
for(j=0;j<100;j++){
if(i==initializeArray[j]){
counter++;
}
}

frequency[i] = counter;
}
cout<<"Number\tValue\tHistogram"<<endl;
for(i=1;i<=10;i++){ //print the histogram
cout<<i<<"\t"<<frequency[i]<<"\t";
for(j=1;j<=frequency[i];j++){
cout<<"*";
}
cout<<"\n";
}

return 0;
}
2. Write C++ program that Treating character arrays as
strings

#include <bits/stdc++.h>

using namespace std;

int main()

char arr[] = { 'A', 'b', 'd', 'u', 'l', 'a', 'z', 'i', 'z'};

int size_arr = sizeof(arr) / sizeof(char);

string str = "";

for (int x = 0; x < size_arr; x++) {

str = str + arr[x];

cout<<"Converted char array to string:\n";

cout << str << endl;

return 0;

}
3. Write C++ program that Passing arrays and individual
array elements to functions

// C++ Program to display numbers of 4 students

#include <iostream>

using namespace std;

// declare function to display Roll Number & take a 1d array as


parameter

void display(int a[4]) {

cout << "Display Roll Numbers: " << endl;

// display array elements

for (int i = 0; i < 4; ++i) {

cout << "Student " << i + 1 << ": " << a[i] << endl;

int main() {

// declare and initialize an array

int rollNum[4] = {4, 1, 3, 2};

display(rollNum); // call display function pass array as argument

return 0;

}
4. Write C++ program that:
 Read two matrix
 Make Addition and multiplication of two matrix
 Print the results

#include<iostream>

using namespace std;

main()

int i,j,a,b, first[10][10], second[10][10], sum[10][10], subtract[10][10],


multiply[10][10];

cout << "Enter the number of rows and columns of matrix : "<<endl;

cin >> a >> b;

cout << "\nEnter the elements of first matrix : "<<endl;

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

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

cin >> first[i][j];

cout << "Enter the elements of second matrix\n"<<endl;

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

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


{

cin >> second[i][j];

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

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

sum[i][j] = first[i][j] + second[i][j];

multiply[i][j] = first[i][j] * second[i][j];

cout << "\nSum of entered matrices : "<<endl;

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

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

cout << sum[i][j] << "\t";

cout << endl;

cout << "\nMultiplication of entered matrices : "<<endl;

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


{

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

cout << multiply[i][j] << "\t";

cout << endl;

return 0;

You might also like