SkillRack PPS8(Varified)

CSE 1002 PPS8
Total time : 600 mins
Challenges : 5

Question 1(Code detection problem)

For security reasons, messages are transmitted as a secret code over a transmission channel. It is usually sent as a sequence of bits, that is, 0s and 1s. Due to noise in the transmission channel, the transmitted message may become corrupted. That is, the message received at the destination may not be the same as the message transmitted. There are several techniques to check the validity of the transmitted message at the destination. One such technique is to transmit the same message twice. At the destination, both copies of the message are compared. Given a file “data.txt” with the messages that is transmitted, write a C++ program to check whether the message received at the destination is error-free. For simplicity, assume that the secret code representing the message is a sequence of digits(0 to 9) and the maximum length of the message is 250 digits. Each original message is followed by a copy message and first number in both the messages indcates the length of the message. Each character in the message is separated by a space. For example, 7 9 2 7 8 3 5 6 7 9 2 7 8 3 5 6 means that the original message is of length 7 and it is 9 2 7 8 3 5 6 and copy message is also of length 7 and it is 9 2 7 8 3 5 6. If orginal and copy message is same then print Message transmitted is ok. Print Message transmitted is not OK when th length of the original or copy message is greater than 250 or when the original message is not same as copy message.
Input Format:
Name of the file
Input file contains a number of secret codes immediately followed their copy
Output Format:
Print either Message transmitted is ok or Message transmitted is not ok

Solution

#include < iostream >
#include < fstream >
#include < string.h >
using namespace std;
int main()
{
    char msg1[250],msg2[250],file_name[20];
    int i,num1,num2,n=0;
    bool ans[20];
    cin>>file_name;
    ifstream fin;
    fin.open(file_name,ios::in);
    while(!fin.eof())
    {
        fin>>num1;
        for(i=0;i < num1;i++)  
        fin>>msg1[i];
        msg1[i]='\0';
        fin>>num2;
        if(num1==num2)
        {
            for(i=0;i < num2;i++)  
            fin>>msg2[i];
            msg2[i]='\0';
            if(strcmp(msg1,msg2)==0)
            ans[n++]=true;
            else
            ans[n++]=false;
        }
        else
        ans[n++]=false;
    }
    fin.close();
    for(int i=0;i < n-1;i++)
    {
        if(ans[i])
        cout<<"Message transmitted ok\n";
        else
        cout<<"Message transmitted is not ok\n";
    }
    return(0);
}

Input

INPUT :

fin.open(file_name,ios::in)

Processing

if(strcmp(msg1,msg2)==0)
    ans[n++]=true;
else
    ans[n++]=false;

Output

OUTPUT :

if(ans[i])
    cout<<"Message transmitted ok\n";
else
    cout<<"Message transmitted is not ok\n";

Pseudocode

BEGIN
Read input
if(strcmp(msg1,msg2)==0)
ans[n++]=true;
else
ans[n++]=false;
if(ans[i])
cout<<“Message transmitted ok\n”;
else
cout<<“Message transmitted is not ok\n”;
END

Question 2(Special pay increase)

Employees in a company are up for a special pay increase. Given a file, with each line consisting of an employee’s last name, first name, current salary, and percent pay increase, write a C++ program to compute the revised pay of each employee.
Miller Andrew     65875.87  5
Green Sheila     75892.56 6
Sethi Amit        74900.50 6.1.
For example, the pay increase for Andrew is 5%.
Input Format:
Name of the input file
The input file contains
last name, first name, salary and percentage of increase for employee1
last name, first name, salary and percentage of increase for employee2

last name, first name, salary and percentage of increase for employeen
Output Format:
last name, first name, updadated salary of employee1 separated by a tab
last name, first name, updadated salary of employee2 separated by a tab
last name, first name, updadated salary of employee3 separated by a tab

Solution

#include
#include
#include
#include
using namespace std;
class employee
{
string lastName;
string firstName;
double salary;
float inc;
double updated_Salary;
public:
//Read lastname firstname salary and inc
void get(ifstream&);
//Print lastname firstname updated salary
void print();
//update salary
void update_Sal();
};
void employee :: get(ifstream& fin)
{
    fin>>lastName>>firstName>>salary>>inc;
}
void employee :: update_Sal()
{
    salary+=((salary*inc)/100);
}
void employee :: print()
{
    update_Sal();
    cout<<lastName<<"\n"<<firstName<<"\n";
    cout<<fixed<<setprecision(2)<<salary<<"\n";  
}  
int main()  
{  
    employee e;  
    char file_name[20];  
    cin>>file_name;
    ifstream fin;
    fin.open(file_name,ios::in);
    while(!fin.eof())
    {
        e.get(fin);
        e.print();
    }
    fin.close();
    return(0);
}

Input

INPUT :

void employee :: get(ifstream& fin)
{
    fin>>lastName>>firstName>>salary>>inc;
}

Processing

salary+=((salary*inc)/100);

Output

OUTPUT :

void print()
{
    update_sal();
    output;
}

Pseudocode

BEGIN

Read input
salary+=((salary*inc)/100);
print()

END

Question 3(Class average)

The marks scored by the students in a test  are stored in a file. The file has the information of the students such as , number, marks scored, and the time taken by the student to complete the test
Student Number          Marks scored         Time taken (in mts)
01          25           40
02           52            38
03           45            60
04            64           52
05           50            50
Write a  C++ program that reads the data in the file created and displays the student’s number, marks scored, time taken by the student to complete the test along with the total marks scored by all the students,   Total time taken by all the students, average marks scored by the students per minute
Input Format:
Name of the file
Input file contains
Student’s number, marks scored, time taken by the student to complete the test, separated by a tab,  of student-1
Student’s number, marks scored, time taken by the student to complete the test, separated by a tab,  of student-2
….
Student’s number, marks scored, time taken by the student to complete the test, separated by a tab,  of student-n
Output Format:
Student’s number, marks scored,Time taken by the student-1, Average mark scored by the student per minute of the test-time, separated by tab.
Student’s number,marks scored,Time taken by the student-2 , Average mark scored by the student per minute of the test-time ,  separated by tab
Student’s number, marks scored,Time taken by the student-n, Average mark scored by the student per minute of the test-time  separated by tab
Total marks scored by all the students
Total time (in minutes) taken by all the students
Sum of the average marks of all the students scored per minute of the test.
Average mark scored (by all the students) per minute of the test-time

Solution

#include < iostream >
#include < fstream >
#include < iomanip >
using namespace std;
int main()
{
    char file_name[20];
    float total_marks=0,total_min=0,total_avg=0,num=0;
    cin>>file_name;
    ifstream fin;
    fin.open(file_name,ios::in);
    while(!fin.eof())
    {
        int a;
        float x,y;
        fin>>a;
        cout<<a<<"\t";  
        fin>>x;
        cout<<fixed<<setprecision(2)<<x<<"\t";  
        total_marks+=x;  
        fin>>y;
        cout<<y<<"\t";
        total_min+=y;
        cout<<x/y<<"\n";
        total_avg+=(x/y);
        num++;
    }
    fin.close();
    float avg=total_marks/total_min;
    cout<<fixed<<setprecision(2)<<total_marks<<"\n"<<total_min<<"\n"<<total_avg<<"\n"<<(total_avg/num);
    return(0);
}

Input

INPUT :

fin.open(file_name,ios::in)

Processing

float avg=total_marks/total_min;

Output

OUTPUT :

cout<<fixed<<setprecision(2)<<total_marks<<"\n"<<total_min<<"\n"<<total_avg<<"\n"<<(total_avg/num);

Pseudocode

BEGIN

Read input
avg=total_marks/total_min;
cout<<fixed<<setprecision(2)<<total_marks<<"\n"<<total_min<<"\n"<<total_avg<<"\n"<<(total_avg/num);

END

Question 4(Sort data in file)

Given a file with a set of numbers in a file, write a C++ program to sort the numbers and print them.
Input Format
File name
In input file, a set of numbers to be sorted each number is separated by a space
Output Format
Numbers in sorted order one number in one line

Solution

#include < iostream >
#include < fstream >
using namespace std;
int main()
{
    char file_name[20];
    int arr[30],num=0;
    cin>>file_name;
    ifstream fin;
    fin.open(file_name,ios::in);
    
    while(!fin.eof())
    fin>>arr[num++];
    fin.close();
    for(int i=0;i < num-1;i++)
    for(int j=i+1;j < num;j++)  
    if(arr[i] > arr[j])
    {
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }
    for(int i=0;i < num;i++)
    cout<<arr[i]<<"\n";
    return(0);
}

Input

INPUT :

fin.open(file_name,ios::in);

Processing

for(int j=i+1;j < num;j++)  
   if(arr[i] > arr[j])
    {
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }

Output

OUTPUT :

for(int i=0;i < num;i++)
cout<<arr[i]<<"\n";

Pseudocode

BEGIN

Read input
for(int j=i+1;j < num;j++)  
   if(arr[i] > arr[j])
    {
        int temp=arr[i];
        arr[i]=arr[j];
        arr[j]=temp;
    }
for(int i=0;i < num;i++)
cout<<arr[i]<<"\n";

END

Question 5(Count words in Tweets)

Given a word ‘w’and a set of tweets in a file named as tweets.txt, write a C++ program to count the number of words in the file. Assume that the program need not bother about case of the words as the input takes care of the same.
Input Format
Word to be searched
Tweets in file
Output Format
Count of the words

Solution

#include < iostream >
#include < fstream >
#include < string.h >
using namespace std;
int main()
{
    int count=0;
    char word[20],search[20];
    cin>>search;
    ifstream fin;
    fin.open("tweets.txt",ios::in  while(!fin.eof())
    {
        fin>>word;
        if(strcmp(word,search)==0)
        count++;
    }
    cout<<count;
    fin.close();
    return(0);
}

Input

INPUT :

fin.open("tweets.txt",ios::in

Processing

if(strcmp(word,search)==0)
count++;

Output

OUTPUT :

cout<<count;

Pseudocode

BEGIN

Read input
if(strcmp(word,search)==0)
count++;
cout<<count;

END
Share on Google Plus

About Unknown

This blog is exclusively for VITIAN'S TO fulfill their needs. Also you can tell our team your need and we will assist you as soon as possible. For any quaries send your emails to "everything4vitians@gmail.com". Thank you
    Blogger Comment

0 comments:

Post a Comment