0% found this document useful (0 votes)
23 views

CP Assignment 4: Answer No.1: Code

The document contains 3 code examples with explanations: 1. A C++ program that defines a struct to store student profiles (name, roll, age, etc.) and uses a for loop to input data for 15 students and output the data. 2. A C++ program that defines a struct to store dates and gets date input for two events, then checks if the dates are the same or different. 3. A short code example of a struct definition for an Employee that is missing a semicolon, preventing it from compiling correctly. The solution adds the missing semicolon.

Uploaded by

Hassan Akhtar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

CP Assignment 4: Answer No.1: Code

The document contains 3 code examples with explanations: 1. A C++ program that defines a struct to store student profiles (name, roll, age, etc.) and uses a for loop to input data for 15 students and output the data. 2. A C++ program that defines a struct to store dates and gets date input for two events, then checks if the dates are the same or different. 3. A short code example of a struct definition for an Employee that is missing a semicolon, preventing it from compiling correctly. The solution adds the missing semicolon.

Uploaded by

Hassan Akhtar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

CP Assignment 4

Answer No.1:
Code:
#include <iostream>
#include <string>
using namespace std;

struct profile
{
int roll;
string name;
int age;
string address;
int marks;
};

profile arr[15];

int main()
{
system("color A");

int i;
int j;

for (i = 0; i < 15; i++)


{
cout << "Enter student " << i + 1 << "'s roll number: ";
cin >> arr[i].roll;

cout << "Enter student " << i + 1 << "'s name: ";
cin >> arr[i].name;

cout << "Enter student " << i + 1 << "'s age: ";
cin >> arr[i].age;

cout << "Enter student " << i + 1 << "'s address: ";
cin >> arr[i].address;

cout << "Enter student " << i + 1 << "'s marks: ";
cin >> arr[i].marks;
cout << endl;
}

for (j = 0; j < 15; j++)


{
cout << "Student " << j << "'s roll number: " << arr[j].roll;
cout << "Student " << j << "'s roll number: " << arr[j].name;

cout << "Student " << j << "'s roll number: " << arr[j].age;

cout << "Student " << j << "'s roll number: " << arr[j].address;

cout << "Student " << j << "'s roll number: " << arr[j].marks;
cout << endl;
}

system("pause");
return 0;
}

Output:
Answer No.2:
Code:
#include <iostream>
#include <string>
using namespace std;

struct date
{
int day;
int month;
int year;
};

int main()
{
system("color A");

date a;
date b;

cout << "Enter day of 1st event: ";


cin >> a.day;

cout << "Enter month of 1st event: ";


cin >> a.month;

cout << "Enter year of 1st event: ";


cin >> a.year;
cout << endl;

cout << "Enter day of 2nd event: ";


cin >> b.day;
cout << "Enter month of 2nd event: ";
cin >> b.month;

cout << "Enter year of 2nd event: ";


cin >> b.year;
cout << endl;

if (a.day == b.day&&a.month == b.month&&a.year == b.year)


{
cout << "Both dates are same.";
}

else
{
cout << "Both dates are different.";
}

cout << endl;

system("pause");
return 0;
}

Output:

Answer No.3
Code:
Consider the following C++ code. What is preventing it from compiling?
struct Employee {
  int id;
  float wage;
}
Output:
ERROR!!!
Solution:
A terminator ( ; ) should be put after the closing bracket
( ; ) because of the rules of the C++ code.
Correction:
Consider the following C++ code. What is preventing it from compiling?
struct Employee {
  int id;
  float wage;
};

A correction can clearly be seen at the end of the entire


code where an additional terminator has been added
after the closing bracket. Now this code can be fitted and
runned successfully.
Name: Daniyal Imran Qureshi Roll no: 02-134211-088
Class: BS(CS) Section: 1-A Date: 18th June 2021

You might also like