[go: up one dir, main page]

0% found this document useful (0 votes)
35 views12 pages

Mohid Mirza 22F-3609: PF Assignment 3

The document contains 10 programming questions and their solutions in C++. The questions cover topics like arrays, strings, searching/replacing substrings, and password validation. Loops and conditional statements are used extensively in the solutions.

Uploaded by

Rafay Naveed
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)
35 views12 pages

Mohid Mirza 22F-3609: PF Assignment 3

The document contains 10 programming questions and their solutions in C++. The questions cover topics like arrays, strings, searching/replacing substrings, and password validation. Loops and conditional statements are used extensively in the solutions.

Uploaded by

Rafay Naveed
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/ 12

pf assignment 3

Mohid mirza 22F-3609


Programming fundamentals

Q1
#include <iostream>
using namespace std;
const int NUM_MONTHS = 12;

int main()
{
double rainfall[NUM_MONTHS] = { 0 };
double total = 0;
double highest = 0;
double lowest = 1000000;
int highMonth = 0;
int lowMonth = 0;

cout << "Enter the total rainfall for each of the 12 months.\n";
for (int i = 0; i < NUM_MONTHS; i++)
{
cout << "Month " << i + 1 << ": ";
cin >> rainfall[i];
total += rainfall[i];

if (rainfall[i] > highest)


{
highest = rainfall[i];
highMonth = i + 1;
}
if (rainfall[i] < lowest)
{
lowest = rainfall[i];
lowMonth = i + 1;
}
}

cout << "\nTotal rainfall for the year: " << total << "\n";
cout << "Average monthly rainfall: " << total / NUM_MONTHS << "\n";
cout << "Month with the highest rainfall: " << highMonth << " with " << highest << " inches.\n";
cout << "Month with the lowest rainfall: " << lowMonth << " with " << lowest << " inches.\n";

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


{
for (int j = 0; j < NUM_MONTHS - i - 1; j++)
{
if (rainfall[j] > rainfall[j + 1])
{
double temp = rainfall[j];
rainfall[j] = rainfall[j + 1];
rainfall[j + 1] = temp;
}
}
}
cout << "\nMonths in ascending order:\n";
for (int i = 0; i < NUM_MONTHS; i++)
{
cout << i + 1 << ": " << rainfall[i] << "\n";
}

return 0;
}
Programming fundamentals

Q2
#include <iostream>

using namespace std;

int main()
{
const int MAX_SIZE = 10;
int arr[MAX_SIZE] = { 0 };
int size = 0;
cout << "Enter up to 10 random integers: ";
for (int i = 0; i < MAX_SIZE; i++)
{
cin >> arr[i];
if (arr[i] == -1)
break;
size++;
}

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


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

{
if (arr[i] == arr[j])
{
for (int k = j; k < size - 1; k++)
{
arr[k] = arr[k + 1];
}
size--;
j--;
}
}
}
cout << "\nArray with duplicates eliminated: ";
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}

return 0;
}

Q3
#include <iostream>
using namespace std;

int main()
{
int marks[100];
int numStudents;
cout << "Enter the number of students: ";
cin >> numStudents;
for (int i = 0; i < numStudents; i++)
{
cout << "Enter the marks of student " << i + 1 << ": ";
Programming fundamentals

cin >> marks[i];


}
double sum = 0;
int minMark = marks[0];
int maxMark = marks[0];
for (int i = 0; i < numStudents; i++)
{
sum += marks[i];
if (marks[i] < minMark)
{
minMark = marks[i];
}
if (marks[i] > maxMark)
{
maxMark = marks[i];
}
}
double avgMark = sum / numStudents;
cout << "The average marks of the students is: " << avgMark << endl;
cout << "The minimum marks of the students is: " << minMark << endl;
cout << "The maximum marks of the students is: " << maxMark << endl;

return 0;
}

Q4
#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
const int NUM_STUDENTS = 100;
int scores[NUM_STUDENTS];

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


{
scores[i] = rand() % 101;
}

int below60 = 0;
Programming fundamentals

int between60and70 = 0;
int between71and80 = 0;
int between81and90 = 0;
int above90 = 0;

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


{
if (scores[i] < 60)
{
below60++;
}
else if (scores[i] >= 60 && scores[i] < 70)
{
between60and70++;
}
else if (scores[i] >= 71 && scores[i] < 80)
{
between71and80++;
}
else if (scores[i] >= 81 && scores[i] < 90)
{
between81and90++;
}
else
{
above90++;
}
}
cout << "Score Range" << setw(15) << "Number of Students" << endl;
cout << "Below 60" << setw(15) << below60 << endl;
cout << "60-70" << setw(15) << between60and70 << endl;
cout << "71-80" << setw(15) << between71and80 << endl;
cout << "81-90" << setw(15) << between81and90 << endl;
cout << "Above 90" << setw(15) << above90 << endl;

return 0;
}

Q5
#include <iostream>
using namespace std;
Programming fundamentals

int main()
{
int sum = 0;
int firstWord = 0;
int secondWord = 0;
int numCharsFirstWord = 0;
int numCharsSecondWord = 0;

std::cout << "Enter the first word: ";


while (true)
{

int ch = cin.get();

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


{
break;
}
sum += ch;

firstWord += ch;
numCharsFirstWord++;
}
cout << "Enter the second word: ";
while (true)
{
int ch = cin.get();
if (ch == ' ' || ch == '\n')
{
break;
}
sum += ch;
secondWord += ch;
numCharsSecondWord++;
}
int thirdWord = 0;
int numCharsThirdWord = 0;
cout << "Enter the third word: ";
while (true)
{
int ch = cin.get();
if (ch == ' ' || ch == '\n')
{
break;
}
thirdWord += ch;
numCharsThirdWord++;
}
if (sum == thirdWord)
{
cout << "The sum of the ASCII values of the first two words is equal to the ASCII value of the third word." <<
endl;
}
else
{
cout << "The sum of the ASCII values of the first two words is not equal to the ASCII value of the third word."
<< endl;
}

return 0;
}
Programming fundamentals

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

int main()
{
cout << "Enter the first 12 digits of an ISBN-13: ";
string isbn;
getline(cin, isbn);
int total = 0;

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


{
if (isbn[i] < '0' || isbn[i] > '9')
{
cout << "Invalid ISBN-13" << endl;
return 0;
}
int digit = isbn[i] - '0';
total += (i % 2 == 0) ? digit * 3 : digit;
}

int checksum = 10 - total % 10;


if (checksum == 10)
{
checksum = 0;
}
cout << "The last digit of the ISBN-13 is: " << checksum << endl;

return 0;
}
Programming fundamentals

Q7
#include <iostream>
#include <string>

using namespace std;

int main()
{

cout << "Enter a string: ";


string input;
getline(cin, input);

string word;
for (int i = 0; i < input.length(); i++)
{
if (input[i] == ' ' || input[i] == '\t')
{
if (word.length() > 0)
{
cout << word << " ";
word = "";
}
}
else
{
word += input[i];
}
}
if (word.length() > 0)
{
cout << word << " ";
}
reverse(input.begin(), input.end());
cout << endl << "Reversed string: " << input;

return 0;
}

Q8
#include <iostream>
Programming fundamentals

#include <string>
using namespace std;
int main()
{
cout << "Enter a string containing multiple sentences: ";
string input;
getline(cin, input);
cout << "Enter a keyword to search for: ";
string keyword;
getline(cin, keyword);
int count = 0;
for (int i = 0; i < input.length(); i++)
{
if (i == 0 || input[i - 1] == ' ' || input[i - 1] == '\t')
{
int j = i;
while (j < input.length() && (input[j] == keyword[j - i] || input[j] == keyword[j - i] - 32))
{
j++;
}

if (j - i == keyword.length())
{
count++;
i += keyword.length() - 1;
}
}
}
cout << "Number of occurrences of the keyword: " << count << endl;
return 0;
}

Q9
#include <iostream>
using namespace std;
int main()
{
cout << "Enter a password: ";
string password;
cin >> password;
int uppercase = 0;
int lowercase = 0;
int digit = 0;
for (int i = 0; i < password.length(); i++)
{

if (password[i] >= 'A' && password[i] <= 'Z')


{
uppercase++;
}
else if (password[i] >= 'a' && password[i] <= 'z')
{
lowercase++;
}
else if (password[i] >= '0' && password[i] <= '9')
Programming fundamentals

{
digit++;
}
}
if (password.length() < 8 || uppercase == 0 || lowercase == 0 || digit == 0)
{
cout << "Password is invalid" << endl;
}
else
{
cout << "Password is valid" << endl;
}
return 0;
}

Q 10

#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter a string containing multiple sentences: ";
string input;
getline(cin, input);
for (int i = 0; i < input.length(); i++)
{
if (input[i] == '.' && i + 1 < input.length() && (input[i + 1] >= 'A' && input[i + 1] <= 'Z'))
{
input[i + 1] += 32;
}
if (i == 0 || (i > 0 && (input[i - 1] == '.' || input[i - 1] == '?' || input[i - 1] == '!')))
{
input[i] -= 32;
Programming fundamentals

}
}
cout << input << endl;
return 0;
}

You might also like