Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
20 views5 pages

Graded Lab 2

The document outlines a graded lab assignment for a C++ programming task, requiring students to write code that handles file operations, dynamic arrays, and functions. The assignment includes specific steps to create functions for reading records from a file, displaying arrays, and filtering even numbers. The deadline for submission is set for June 3, 2024, at 2:00 PM, with a total of 20 marks available for completion.

Uploaded by

zaryabimran222
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)
20 views5 pages

Graded Lab 2

The document outlines a graded lab assignment for a C++ programming task, requiring students to write code that handles file operations, dynamic arrays, and functions. The assignment includes specific steps to create functions for reading records from a file, displaying arrays, and filtering even numbers. The deadline for submission is set for June 3, 2024, at 2:00 PM, with a total of 20 marks available for completion.

Uploaded by

zaryabimran222
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/ 5

NAME: Ahmad Shahzad

REG NUMBER: L1F23BSCS0295

Graded Lab 2
(PF-B12)
Topics Covered: Functions, Pointers, file handling

[10-Points]

Write C++ code for the following steps and submit only one .doc file on Portal
within specified time.
Deadline: 2:00 PM on Monday (03-June-2024)
Marks 20= 3+3+3+3+4+4

Task 1
Note: A file named “myfile.txt” have following data 23,22,11,34,45 and so on

Write a program which performs the following tasks

Step 1: Write a user define function named “int getRecords(char*)” which


receives the name of file”myfile.txt” and return the number of records in file in
main().

Step 2: Write a user define function named “int* dynamic_array(int)” which


receives the number of records from step 1 and create dynamic array on heap

Step 3: Write a user define function named "void show_Array(int*,int)" which


receive the array and size of array created in step 2, and show the all elements of
array
Step 4: Write a user define function named "int *get_Array(int*,int)" which
receive the array and size of array created in step 2, and return the complete
array in main() function

Step 5: Write a user define function named "int* even_array(int*,int)" which


receive the array and return the all even elements of array create in step 4.

Step 6: Write a user define function named "void show_Array(int*,int)" which


receive the array and size of array created in step 5, and show the all elements of
array

SOLUTION:

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

int getRecords(string fname);

int *dynamic_array(int count, string fname);

void show_Array(int array[], int count);

int *get_Array(int array[], int count);

int *even_array(int array[], int count, int * evencount);

void show_EvenArray(int array[], int evencount);

int main()
{
string fname;
cout << "Enter the name of the file (myfile.txt) : ";
getline(cin, fname);

int count = getRecords(fname);


cout << endl;
cout << "Total Number of Elements in your File : " << count << endl;
int* array = dynamic_array(count, fname);
cout << endl;

show_Array(array, count);
cout << endl;

int* mainArray = get_Array(array, count);

cout << endl;


cout << "Values in Array From Main: ";
for (int i = 0; i < count; i++)
{
cout << mainArray[i] << " ";
}
cout << endl;

int evencount = 0;
int* EvenArray = even_array(mainArray, count, &evencount);

cout << endl;


show_EvenArray(EvenArray, evencount);
cout << endl;

system("pause");
return 0;
}

int getRecords(string fname)


{
int n, count = 0;
ifstream read;
read.open(fname);
if (read.is_open())
{
while (1)
{
read >> n;
count++;
if (read.eof())
{
break;
}
}
}
else
{
cout << "Unable to Open Your File!" << endl;
}
read.close();
return count;
}

int *dynamic_array(int count, string fname)


{
int *dynamic_array = new int [count];

int n, i=0;
ifstream read;
read.open(fname);
if (read.is_open())
{
while (1)
{
read >> n;
dynamic_array[i] = n;
i++;
if (read.eof())
{
break;
}
}
}
else
{
cout << "Unable to Open Your File!" << endl;
}

return dynamic_array;
}

void show_Array(int array[], int count)


{
cout << "All Elements Stored in the Array From your given File's Data: ";
for (int i = 0; i < count; i++)
{
cout << array[i] << " ";
}
}

int *get_Array(int array[], int count)


{
return array;
}

int *even_array(int array[], int count, int* evencount)


{
int Evencount = 0;
for (int i = 0; i < count; i++)
{
if (array[i] % 2 == 0)
{
Evencount++;
}
}

int *evenarray = new int[Evencount];


int j = 0;
for (int i = 0; i < count; i++)
{
if (array[i] % 2 == 0)
{
evenarray[j] = array[i];
j++;
}
}
*evencount = Evencount;
return evenarray;
}

void show_EvenArray(int array[], int evencount)


{
cout << "All the Even Elements in your Array Readen from Your given File :
";
for (int i = 0; i < evencount; i++)
{
cout << array[i] << " ";
}
cout << endl;
}

You might also like