C++ Code Snippets: Array Manipulation and File I/O, Assignments of Object Oriented Programming

Five C++ code snippets demonstrating array manipulation using dynamic memory allocation and file I/O using the standard library. The first code snippet initializes, prints, and deep copies an integer array. The second code snippet reads student marks from a text file and stores the data in dynamic arrays. The third code snippet copies a character string to a dynamic array. The fourth code snippet performs matrix addition using dynamic memory allocation. The fifth code snippet inputs and displays a 2D integer matrix with varying row sizes.

Typology: Assignments

2020/2021

Uploaded on 06/21/2021

muhammad-faizan-mughal
muhammad-faizan-mughal 🇵🇰

5 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Code 1
#include <iostream>
using namespace std;
void initializeArray(int *x, int size)
{
for (int i = 0; i<size; i++)
{
x[i] = 3 * i;
}
}
void printArray(int *x, int size)
{
for (int i = 0; i<size; i++)
{
cout << x[i] << "\t";
}
cout << endl;
}
int * deepCopy(int *x, int size)
{
int secondsize = size;
int *x2 = new int[secondsize];
for (int i = 0; i < size; ++i)
{
x2[i] = x[i];
}
return x2;
}
int main()
{
int size;
cout << "PLEASE ENTER THE SIZE :" ;
cin >> size;
int *array = new int[size];
initializeArray(array, size);
cout << "FIRST ARRAY: " ;
printArray(array, size);
int *secondarray = deepCopy(array, size);
cout << "COPIED ARRAY: " ;
printArray(secondarray, size);
delete[] array;
cout << "DELETING FIRST ARRAY...\nAFTER DELETING FIRST ARRAY ,COPIED ARRAY
IS STILL SAFE " << endl;
printArray(secondarray, size);
delete[] secondarray;
system("pause");
return 0;
}
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C++ Code Snippets: Array Manipulation and File I/O and more Assignments Object Oriented Programming in PDF only on Docsity!

Code 1

#include using namespace std; void initializeArray(int *x, int size) { for (int i = 0; i<size; i++) { x[i] = 3 * i; } } void printArray(int *x, int size) { for (int i = 0; i<size; i++) { cout << x[i] << "\t"; } cout << endl; } int * deepCopy(int *x, int size) { int secondsize = size; int *x2 = new int[secondsize]; for (int i = 0; i < size; ++i) { x2[i] = x[i]; } return x2; } int main() { int size; cout << "PLEASE ENTER THE SIZE :" ; cin >> size; int *array = new int[size]; initializeArray(array, size); cout << "FIRST ARRAY: " ; printArray(array, size); int *secondarray = deepCopy(array, size); cout << "COPIED ARRAY: " ; printArray(secondarray, size); delete[] array; cout << "DELETING FIRST ARRAY...\nAFTER DELETING FIRST ARRAY ,COPIED ARRAY IS STILL SAFE " << endl; printArray(secondarray, size); delete[] secondarray; system("pause"); return 0; }

Output

Code 2

#include #include #include using namespace std; int main() { int i = 0; int j = 0; int k = 0; int Students; int Labs; int n = 0; bool loop = true; char character; string temp; bool read = false; fstream file; file.open("students marks.txt", ios::in); if (file) { while (file.good()) { if (!read) { file >> character; } if (n == 0 && character == ':') { file >> Students; n++; character = ' ';

if (Marks[i][j] >= 50 && Marks[i][j] <= 79) { Grades[i][j] = 'C'; } if (Marks[i][j] >= 33 && Marks[i][j] <= 49) { Grades[i][j] = 'D'; } if (Marks[i][j] >= 0 && Marks[i][j] <= 32) { Grades[i][j] = 'F'; } } } k++; } for (i = 0; i < Students; i++) { for (j = 0; j < Labs + 1; j++) { cout << Grades[i][j] << " "; } cout << endl; } loop = false; } if (!loop) { break; } } } } else { cout << "Files does not find "; } file.close(); system("pause"); }

Output

Code 4

#include using namespace std; void Input(int** p, int row, int colum) { for (int i = 0; i<row; i++) { for (int j = 0; j<colum; j++) { cin >> p[i][j]; } } } int** Sum(int** p, int row, int colum, int ** q, int secondrow, int secondcolum ) { int **sum = new int *[row]; for (int i = 0; i<row; i++) sum[i] = new int[i]; for (int i = 0; i < row; ++i) { for (int j = 0; j < colum; ++j) { sum[i][j] = p[i][j] + q[i][j]; } } return sum; } void Display(int **sum, int row, int colum) { for (int i = 0; i<row; i++) { for (int j = 0; j<colum; j++) { cout << sum[i][j] << " "; } cout << endl;

cout << endl; } int main() { int sizeoffirstmatrix; int sizeofsecondmatrix; cout << "Size of first matrix:"; cin >> sizeoffirstmatrix; cout << "Size of second matrix:"; cin >> sizeofsecondmatrix; if (sizeoffirstmatrix == sizeofsecondmatrix) { int **matrix1 = new int *[sizeoffirstmatrix]; int **matrix2 = new int *[sizeofsecondmatrix]; for (int i = 0; i<sizeoffirstmatrix; i++) { matrix1[i] = new int[sizeoffirstmatrix]; matrix2[i] = new int[sizeofsecondmatrix]; } cout << " entries of first matrix" << endl; Input(matrix1, sizeoffirstmatrix, sizeoffirstmatrix); Display(matrix1, sizeoffirstmatrix, sizeofsecondmatrix); cout << " entries of second matrix" << endl; Input(matrix2, sizeoffirstmatrix, sizeoffirstmatrix); Display(matrix2, sizeofsecondmatrix, sizeofsecondmatrix); cout << " ADDITION" << endl; int **sum = Sum(matrix1, sizeoffirstmatrix, sizeoffirstmatrix, matrix2, sizeofsecondmatrix, sizeofsecondmatrix); Display(sum, sizeoffirstmatrix, sizeoffirstmatrix); } else cout << "error!! size of matrices are not same \n"; system("pause"); return 0; }

Output

for (int j = 0; j<colum[i]; j++) { cout << matrix[i][j] << " "; } cout << endl; } cout << endl; } int main() { int row, colum; cout << "Enter the Total Rows :"; cin >> row; int **matrix = new int *[row]; int *r = new int[row]; for (int i = 1; i<row; i++) { cout << "Enter colum of row " << i << ":"; cin >> colum; r[i] = colum; matrix[i] = new int[colum]; } cout << endl; Input(matrix, row, r); cout << "Matrix is ... " << endl; Display(matrix, row, r); system("pause"); return 0; }

Output