0% found this document useful (0 votes)
47 views13 pages

Fa24 Bcs 001 Assignment 4

Struct and file assignment

Uploaded by

harisabid88.1
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)
47 views13 pages

Fa24 Bcs 001 Assignment 4

Struct and file assignment

Uploaded by

harisabid88.1
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/ 13

Task 1

#include <iostream>

#include <string>

using namespace std;

struct studentType {

string studentName;

string studentLName;

int testScore;

char grade;

};

void readStudentData(studentType students[], int size) {

for (int i = 0; i < size; i++) {

cout << "Enter details for Student " << i + 1 << ":\n";

cout << "First Name: ";

cin >> students[i].studentName;

cout << "Last Name: ";

cin >> students[i].studentLName;

cout << "Test Score: ";

cin >> students[i].testScore;

while (students[i].testScore < 0 || students[i].testScore > 100) {


cout << "Invalid score! Enter a value between 0 and 100: ";

cin >> students[i].testScore;

void assignGrades(studentType students[], int size) {

for (int i = 0; i < size; i++) {

if (students[i].testScore >= 90)

students[i].grade = 'A';

else if (students[i].testScore >= 80)

students[i].grade = 'B';

else if (students[i].testScore >= 70)

students[i].grade = 'C';

else if (students[i].testScore >= 60)

students[i].grade = 'D';

else

students[i].grade = 'F';

int findHighestScore(studentType students[], int size) {

int highest = students[0].testScore;

for (int i = 1; i < size; i++) {

if (students[i].testScore > highest) {

highest = students[i].testScore;
}

return highest;

void printStudentsWithHighestScore(studentType students[], int size, int


highestScore) {

cout << "Students with the highest test score:\n";

for (int i = 0; i < size; i++) {

if (students[i].testScore == highestScore) {

cout << students[i].studentName << " " <<


students[i].studentLName << endl;

int main() {

const int SIZE = 20;

studentType students[SIZE];

readStudentData(students, SIZE);

assignGrades(students, SIZE);
int highestScore = findHighestScore(students, SIZE);

cout << "\nStudent data:\n";

for (int i = 0; i < SIZE; i++) {

cout << students[i].studentName << " " << students[i].studentLName

<< ": Test Score: " << students[i].testScore

<< ", Grade: " << students[i].grade << endl;

cout << "\nHighest Test Score: " << highestScore << endl;

printStudentsWithHighestScore(students, SIZE, highestScore);

return 0;
}
Task 2

#include<iostream>

#include <string>

#include <fstream>

using namespace std;

void read_file(string& str)

ifstream in("data.txt");

if (!in)

cout << "Error!! unable to open the file." << endl;

return;

getline(in, str);

in.close();

string remove_punctuations(const string& str, int size)

string cleared_sentence = "";

for (int i = 0; i < size; ++i)

char ch = str[i];

if (ch != ',' && ch != '.' && ch != ';' && ch != ':' && ch != '!' && ch

!= '?')

cleared_sentence += ch;
}

return cleared_sentence;

void output_file(string& str, int size)

ofstream out("output.txt");

if (!out)

cout << "Error!! unable to open file." << endl;

return;

string new_str = "";

for (int i = 0; i < size; ++i)

char ch = str[i];

if (ch == ' ' || ch == '\n')

if (!new_str.empty())

out << new_str << " " << new_str.length() << endl;

new_str = "";

else

new_str += ch;
}

if (!new_str.empty())

out << new_str << " " << new_str.length() << endl;

out.close();

int main()

string str;

read_file(str);

int length_of_string = str.length();

string cleaned_sentence = remove_punctuations(str, length_of_string);

int length_of_cleanedSentence = cleaned_sentence.length();

output_file(cleaned_sentence, length_of_cleanedSentence);

return 0;

Output

Data. txt=
This is a test

Result is
Task 3
#include<iostream>

#include<string>

#include <fstream>

using namespace std;

void read_file(string& str1, string& str2)

ifstream in("input.txt");

if (!in)

cout << "Error!! unable to open the file." << endl;

return;

if (!getline(in, str1))

cout << "Error to read the First Line." << endl;


}

if (!getline(in, str2))

cout << "Error to read the Second Line." << endl;

in.close();

void output_file(string& str1, string& str2, int size1, int size2)

ofstream out("output.txt");

if (!out)

cout << "Error!! unable to open the file." << endl;

return;

int min_length = (size1 < size2) ? size1 : size2;

for (int i = 0; i < min_length; ++i)

if (str1[i] == str2[i])

out << str1[i];

out << " ";

for (int i = 0; i < min_length; ++i)

if (str1[i] == str2[i])
{

out << i + 1<<" ";

out.close();

int main()

string str1;

string str2;

read_file(str1, str2);

int length_1 = str1.length();

int length_2 = str2.length();

output_file(str1, str2, length_1, length_2);

return 0;

Output:

Input.txt=
processing
programming

Result

You might also like