Assignment 3 PF
Assignment 3 PF
Code:
#include <iostream>
#include <fstream>
using namespace std;
char *dynamic(int size);
int *intdynamic(int size);
void readfromfile(char *word, int size);
int length(char *word);
void display(int *len, int index);
int main(){
char *word=NULL;
int size = 100;
word = dynamic(size);
readfromfile(word,size);
return 0;
}
char *dynamic(int size){
char *a = new char[size];
return a;
}
int *intdynamic(int size){
int *a = new int[size];
return a;
}
void readfromfile( char *word, int size){
ifstream fin;
char *filename;
filename = dynamic(size);
cout << "Enter Filename: ";
cin >> filename;
fin.open(filename);
int *len=NULL;
len = intdynamic(size);
int i = 0;
while (!fin.eof()){
fin.getline(word, 100, ' ');
len[i] = length(word);
i++;
}
display(len, i);
}
int length(char *word){
int len = 0;
for (int i = 0; word[i] != '\0'; i++){
len++;
}
return len;
}
void display(int *len, int index){
cout << "Array of lengths: ";
for (int i = 0; i < index; i++){
cout <<len[i]<<" ";
}
cout << endl;
}
Output:
Question no 2:
Code:
#include <iostream>
using namespace std;
int *dynamic(int col);
int **dynamic2(int row);
void matrix_multi(int row1, int col1, int col2, int**first_matrix,
int**second_matrix);
int main(){
int **first_matrix = NULL;
int **second_matrix = NULL;
int rows1 = NULL;
int rows2 = NULL;
int col1 = NULL;
int col2 = NULL;
cout << "Enter rows of first matrix: " << endl;
cin >> rows1;
cout << "Enter column of first matrix: " << endl;
cin >> col1;
cout << "Enter rows of second matrix: " << endl;
cin >> rows2;
cout << "Enter column of second matrix: " << endl;
cin >> col2;
first_matrix = dynamic2(rows1);
second_matrix = dynamic2(rows2);
if (col1 == rows2){
cout << "Enter the values of first matrix: " << endl;
for (int i = 0; i < rows1; i++){
first_matrix[i] =dynamic(col1);
for (int j = 0; j < col1; j++){
cin >> first_matrix[i][j];
}
}
cout << "Enter the value of second matrix: " << endl;
for (int i = 0; i < rows2; i++){
second_matrix[i] = dynamic(col2);
for (int j = 0; j < col2; j++){
cin >> second_matrix[i][j];
}
}
matrix_multi(rows1, col1, col2, first_matrix, second_matrix);
}
else{
cout << "You entered wrong rows and column!!" << endl;
}
}
Output:
Question no 4:
Code:
#include <iostream>
using namespace std;
int *dynamic(int col);
int **dynamic2(int row);
void max(int rows, int col, int **matrix);
int main(){
int col = NULL;
int rows = NULL;
int **matrix = NULL;
cout << "Enter number of rows: " << endl;
cin >> rows;
cout << "Enter number of column: " << endl;
cin >> col;
matrix = dynamic2(rows);
Output:
Question no 5:
Code:
#include <iostream>
using namespace std;
int *dynamic(int col);
int **dynamic2(int row);
void display(int row, int col, int**matrix);
void replace(int row, int col, int **matrix);
int main(){
int col = NULL;
int rows = NULL;
int **matrix = NULL;
cout << "Enter number of rows: " << endl;
cin >> rows;
cout << "Enter number of column: " << endl;
cin >> col;
matrix = dynamic2(rows);
cout << "Enter the value of matrix: " << endl;
for (int i = 0; i < rows; i++){
matrix[i] = dynamic(col);
for (int j = 0; j < col; j++){
cin >> matrix[i][j];
}
}
display( rows, col,matrix);
replace(rows,col,matrix);
cout << "Matrix after replace: " << endl;
display( rows, col, matrix);
return 0;
}
int *dynamic(int col){
int*x = new int[col];
return x;
}
int **dynamic2(int row){
int**y = new int*[row];
return y;
}
void replace(int row, int col,int **matrix){
int c;
cout << "Enter column number: " << endl;
cin >> c;
int *mat = dynamic(col);
cout << "Enter " << col << " values "<<endl;
for (int i = 0; i < col; i++){
cin >> mat[i];
}
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
matrix[i][c] = mat[i];
}
}
}
void display(int row, int col, int**matrix){
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
cout << matrix[i][j] << " ";
}
cout << endl;
}
}
Output: