0% found this document useful (0 votes)
4 views

Assignment 3 PF

The document contains multiple C++ code snippets that demonstrate dynamic memory allocation for arrays and matrices. Each code snippet includes functions for reading data from user input, performing matrix operations such as multiplication, transposition, finding the maximum value, and replacing a column in a matrix. The main function in each snippet orchestrates user input and calls the relevant functions to execute the desired operations.

Uploaded by

arazameysam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment 3 PF

The document contains multiple C++ code snippets that demonstrate dynamic memory allocation for arrays and matrices. Each code snippet includes functions for reading data from user input, performing matrix operations such as multiplication, transposition, finding the maximum value, and replacing a column in a matrix. The main function in each snippet orchestrates user input and calls the relevant functions to execute the desired operations.

Uploaded by

arazameysam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Q

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;
}
}

int *dynamic(int col){


int*x = new int[col];
return x;
}
int **dynamic2(int row){
int**y = new int*[row];
return y;
}
void matrix_multi(int row1,int col1, int col2, int**first_matrix,
int**second_matrix){
int**temp = NULL;
temp = dynamic2(row1);
for (int i = 0; i < row1; i++){
temp[i] = dynamic(col1);
}
for (int i = 0; i < row1; i++){
for (int j = 0; j < col2; j++){
temp[i][j] = 0;
for (int k = 0; k < col1; k++){
temp[i][j] = (first_matrix[i][k] * second_matrix[k]
[j]) + temp[i][j];
}
}
}
cout << "Multiplication of the matrix: " << endl;
for (int i = 0; i < row1; i++){
for (int j = 0; j < col2; j++){
cout << temp[i][j] << " ";
}
cout << endl;
}
}
Output:
Question no 3:
Code:
#include <iostream>
using namespace std;
int *dynamic(int col);
int **dynamic2(int row);
void transpose(int row, int col, int** Matrix_1);
int main(){
int **Matrix_1 = NULL;
int rows = NULL;
int col = NULL;

cout << "Enter number of rows: " << endl;


cin >> rows;
cout << "Enter number of columns: " << endl;
cin >> col;
Matrix_1 = dynamic2(rows);
cout << "Enter the value of matrix: " << endl;
for (int i = 0; i < rows; i++){
Matrix_1[i] = dynamic(col);
for (int j = 0; j < col; j++){
cin >> Matrix_1[i][j];
}
}
transpose( rows,col, Matrix_1);
}
int *dynamic(int col){
int*x = new int[col];
return x;
}
int **dynamic2(int row){
int**y = new int*[row];
return y;
}
void transpose(int row, int col, int** Matrix_1)
{
int ro = col;
int co = row;
int**temp = NULL;
temp = dynamic2(ro);
for (int i = 0; i < ro; i++){
temp[i] = dynamic(co);
for (int j = 0; j < co; j++){
temp[i][j] = Matrix_1[j][i];
}
}
cout << "Transpose of matrix: " << endl;
for (int i = 0; i < ro; i++){
for (int j = 0; j < co; j++){
cout << temp[i][j] << " ";
}
cout << 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);

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];
}
}
max(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 max(int rows, int col, int **matrix)
{
int max = matrix[0][0];
int r, c;
for (int i = 0; i < rows; i++){
for (int j = 0; j < col; j++){
if (max < matrix[i][j]){
max = matrix[i][j];
r = i;
c = j;
}
}
}
cout << max<<" at location "<<r<<" "<<c<<endl;
}

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:

You might also like