Mohid Mirza 22F-3609: PF Assignment 3
Mohid Mirza 22F-3609: PF Assignment 3
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];
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";
return 0;
}
Programming fundamentals
Q2
#include <iostream>
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++;
}
{
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
return 0;
}
Q4
#include <iostream>
#include <iomanip>
int main()
{
const int NUM_STUDENTS = 100;
int scores[NUM_STUDENTS];
int below60 = 0;
Programming fundamentals
int between60and70 = 0;
int between71and80 = 0;
int between81and90 = 0;
int above90 = 0;
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;
int ch = cin.get();
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;
return 0;
}
Programming fundamentals
Q7
#include <iostream>
#include <string>
int main()
{
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++)
{
{
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;
}