Lab Manual 14+15
Lab Manual 14+15
Example 1
#include <iostream>
using namespace std;
void display(int marks[5]);
int main() {
int marks[5] = {88, 76, 90, 61, 69};
display(marks);
return 0;
}
void display(int m[]) {
cout<<"Displaying marks: "<<endl;
for (int i = 0; i <5; ++i) {
cout<<"Student "<<i+1<<": "<<m[i]<<endl;
}
}
OUTPUT:
Example 2:
#include <iostream>
using namespace std;
/* This function adds the corresponding
* elements of both the arrays and
* displays it.
*/
void sum(int arr1[], int arr2[]){
int temp[5];
for(int i=0; i<5; i++){
temp[i] = arr1[i]+arr2[i];
cout<<temp[i]<<endl;
}
}
int main(){
int a[5] = {10, 20, 30, 40 ,50};
int b[5] = {1, 2, 3, 4, 5};
//Passing arrays to function
sum(a, b);
return 0;
}
OUTPUT:
Lab Tasks
Task 1
(Print an array) Write a function print Array that takes an array and the size of the array as arguments,
prints the array, and returns nothing. The function should stop processing and return when it receives an
array of size zero.
Task 2
Write a program that reads from the user two arrays of the same size:
Array A: contains double numbers representing the students’ lab marks
Array B: contains integer numbers representing the total absences for each student.
The program then calls a function called (calcTotal) that will take array A and B as an input and returns
an array C of type double that contains the result of subtracting each element in B from the corresponding
element in A.
Ex:
Array A 78.0 90.0 55.5 85.7 99.0
Array B 5 3 2 0 3
Task 3
Write a C++ program to get 15 POSITIVE integer values from user and
Store these values into an array if and only if the new value does not exist into the array.
If the value exists into the array and array is not full, your program should display “Sorry: Value Already
Exist”.
Note: Following content is taken from w3school
There are three classes included in the fstream library, which are used to
create, write or read files:
Class Description
The three objects, that is, fstream, ofstream, and ifstream, have the open()
function defined in them. The function takes this syntax:
open (file_name, mode);
Value Description
ios:: app The Append mode. The output sent to the file is appended to it.
ios::ate It opens the file for the output then moves the read and write control to file’s end
ios::trunc If a file exists, the file elements should be truncated prior to its opening.
It is possible to use two modes at the same time. You combine them using the
| (OR) operator.
Example 1:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
fstream my_file;
my_file.open("my_file", ios::out);
if (!my_file) {
cout << "File not created!";
}
else {
cout << "File created successfully!";
my_file.close();
}
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main() {
// Create and open a text file
ofstream MyFile("filename.txt");
Read a File
To read from a file, use either the ifstream or fstream class, and the name of the
file.
Note that we also use a while loop together with the getline() function (which
belongs to the ifstream class) to read the file line by line, and to print the
content of the file:
// Create a text string, which is used to output the text file
string myText;
// Use a while loop together with the getline() function to read the file line by line
while (getline (MyReadFile, myText)) {
// Output the text from the file
cout << myText;
}