0% found this document useful (0 votes)
6 views8 pages

Lab Manual 14+15

The lab manual covers advanced topics on arrays and functions in C++, including examples of passing arrays to functions and performing operations on them. It includes tasks for students to implement functions for printing arrays, calculating totals based on user input, and managing unique positive integers in an array. Additionally, it introduces file handling using the fstream library, detailing how to create, write, and read files in C++.

Uploaded by

groundedblogger1
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)
6 views8 pages

Lab Manual 14+15

The lab manual covers advanced topics on arrays and functions in C++, including examples of passing arrays to functions and performing operations on them. It includes tasks for students to implement functions for printing arrays, calculating totals based on user input, and managing unique positive integers in an array. Additionally, it introduces file handling using the fstream library, detailing how to create, write, and read files in C++.

Uploaded by

groundedblogger1
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
You are on page 1/ 8

Lab Manual (Lab 14+15)

Session: Spring 2021

School of Systems and Technology


UMT Lahore Pakistan

LAB INSTRUCTOR: AYESHA MAJID ALI


Contact: [email protected] Room #: 504 Last cabin 4th floor SST
More on arrays & Functions
Objectives
• Understanding the working of arrays.
• Passing arrays to a function.

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

Array C 73.0 87.0 53.5 85.7 96.0

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

LAB MANUAL 15:


C++ Files
The fstream library allows us to work with files.

To use the fstream library, include both the


standard <iostream> AND the <fstream> header file:

There are three classes included in the fstream library, which are used to
create, write or read files:

Class Description

ofstream Creates and writes to files

ifstream Reads from files

fstream A combination of ofstream and ifstream: creates, reads, and


writes to files
The following image makes it simple to understand:

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

 The file_name parameter denotes the name of the file to open.


 The mode parameter is optional. It can take any of the following values:

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::in It opens the file for a read.

ios::out It opens the file for a write.

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

Create and Write to a File


To create a file, use either the ofstream or fstream class, and specify the name of
the file.

To write to the file, use the insertion operator (<<).

#include <iostream>
#include <fstream>
using namespace std;

int main() {
// Create and open a text file
ofstream MyFile("filename.txt");

// Write to the file


MyFile << "Files can be tricky, but it is fun enough!";

// Close the file


MyFile.close();
}

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;

// Read from the text file


ifstream MyReadFile("filename.txt");

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

// Close the file


MyReadFile.close();

You might also like