Nothing Special   »   [go: up one dir, main page]

PSCP Assignment-6 Due Date: March 5th, 2021 Section-C

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 11

PSCP Assignment-6

Due Date: March 5th, 2021 Section-C

1)Use a double-subscripted array to solve the following problem. A company has four salespeople
(1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passes in a slip for
each different type of product sold. Each slip contains the following:

a) The salesperson number


b) The product number
c) The total dollar value of that product sold that day

Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information
from all of the slips for last month is available. Write a program that will read all this information
for last month’s sales and summarize the total sales by salesperson by product. All totals should
be stored in the double-subscripted array sales. After processing all the information for last month,
print the results in tabular format with each of the columns representing a particular salesperson
and each of the rows representing a particular product. Cross total each row to get the total sales
of each product for last month; cross total each column to get the total sales by salesperson for last
month. Your tabular printout should include these cross totals to the right of the totaled rows and
to the bottom of the totaled columns.

Ans.

#include <iostream>
#include <iomanip>
using namespace std;

int main(void){
int num_sales_person=4;
int num_products=5;
int query_salesman;
int query_product;
int query_dollar;
float array[num_sales_person+1][num_products+1]{};
float trans_array[num_products+1][num_sales_person+1]{};
cout<<"Enter -1 to end the loop"<<endl;
while(true){
cout<<"Enter salesman number, product number and dollar value of product sold that
day"<<endl;
cin>>query_salesman>>query_product>>query_dollar;
if(query_salesman == -1){
break;
}
array[query_salesman-1][query_product-1] = query_dollar;
}
for(int i = 0; i < num_sales_person; i++){
for(int j = 0; j<num_products; j++){
array[i][num_products] += array[i][j];
array[num_sales_person][j] += array[i][j];
}
}
for (int i = 0; i <= num_sales_person; ++i){
for (int j = 0; j <= num_products; ++j){
trans_array[j][i] = array[i][j];
}
}
cout<<"\t\t";
for(int i=0; i<num_sales_person; i++){
cout<<"Salesman "<<i+1<<"\t";
}
cout<<"Total"<<endl;

trans_array[num_products][num_sales_person] = -1;
for(int i = 0; i <= num_products; i++){
if(i<num_products){
cout<<"Product "<<i+1<<"\t";
}
else{
cout<<"Total\t\t";
}
for(int j = 0;j <= num_sales_person; j++){
if(trans_array[i][j] != -1){
cout<<setw(4)<<trans_array[i][j]<<"\t\t";
}
}
cout<<endl;
}
return 0;
}
2) Two frogs are sitting at the bottom of a flight of 10 steps and debating in how many ways then
can jump up the stairs. They can jump one, two or three steps at once. For example, they can cover
the 10 steps by jumping (3,3,3,1) or (2,3,2,1,2) or other suitable combinations of steps. Their
mathematics is not very strong and they approach you for help in order to find out the total number
of possibilities they have to reach the top. Please provide them with a general solution (not only
for 10 but for general n steps) in the form of a program Note that the order of the steps is important
here, i.e., (3,3,3,1) is treated distinct from (1,3,3,3) for example.

Ans.

#include <iostream>
using namespace std;

int steps(int n);

int main(void){
int num_steps;
cin>>num_steps;
cout<<steps(num_steps)<<endl;
}

int steps(int n){


if(n == 1){
return n;
}
else if(n==2){
return n;
}
else if(n==3){
return 4;
}
return steps(n-1) + steps(n-2) + steps(n-3);
}
Ans.

#include <iostream>
using namespace std;

int main(void){
int m_rows, n_columns;
cin>>m_rows>>n_columns;
int array[m_rows][n_columns];
for(int i = 0; i<m_rows; i++){
for(int j = 0; j<n_columns; j++){
cin>>array[i][j];
}
}
for(int i = 0; i<m_rows; i++){
for(int j = 0; j<n_columns; j++){
if(array[i][j] != 0){
cout<<"<"<<i<<", "<<j<<", "<<array[i][j]<<">"<<endl;
}
}
}
return 0;
}
4) Given an array of integers. Find a peak element in it. An array element is peak if it is NOT
smaller than its neighbors. For corner elements, we need to consider only one neighbor. For
example, for input array {5, 10, 20, 15}, 20 is the only peak element. For input array {10, 20, 15,
2, 23, 90, 67}, there are two peak elements: 20 and 90. Write a program to print all the peak
elements in a given array.

Ans.

#include <iostream>
using namespace std;

bool peak_element(int array[], int length, int i);

int main(void){
int length;
cin>>length;
int array[length];
for(int i = 0; i<length; i++){
cin>>array[i];
}
for(int i = 0; i<length; i++){
if(peak_element(array, length, i)==true){
cout<<array[i]<<endl;
}
}
}

bool peak_element(int array[], int length, int i){

if(i == 0){
if(array[i+1]<array[i]){
return true;
}
}
else if(i == length-1){
if(array[i-1]<array[i]){
return true;
}
}
else{
if(array[i-1]<array[i] && array[i+1]<array[i]){
return true;
}
}
return false;
}

5) A rich man died. In his will, he has divided his gold coins among his 5 sons, 5 daughters and a
manager. According to his will: First give one coin to manager. 1/5th of the remaining to the elder
son. Now give one coin to the manager and 1/5th of the remaining to second son and so on. After
giving coins to 5th son, divide the remaining coins among five daughters equally. All should get
full coins. Find the minimum number of coins he has? Write a program to display the number of
coins that each of his sons, daughters and the manager got?

Ans.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main(void){
int num_son = 5;
int num_daughter = 5;
int each_daughter;
int num_of_coins = 1;

//calculating num of coins


while(true){
if((num_of_coins-1+num_son) % (int)pow(5, num_son) == 0 && (int)(1+
(num_of_coins-1+num_son)*(pow(0.8, num_son)))%num_daughter == 0){
break;
}
num_of_coins++;
}
cout<<"Min number of coins: "<<num_of_coins<<endl;
int spent = 0;
int remainder = num_of_coins;
//dividing it among them
for(int i = 0; i<num_son; i++){
spent +=1;
cout<<"Share for son "<<i+1<<" is:"<<(pow(0.8, i))*((num_of_coins-
1+num_son)/5)<<endl;
spent += (pow(0.8, i))*((num_of_coins-1+num_son)/5);
remainder = num_of_coins - spent;

}
each_daughter = remainder/num_daughter;
cout<<"Manager's share: "<<num_son<<endl;
cout<<"Each daughter share:"<<each_daughter<<endl;
return 0;
}

Ans.

#include <iostream>
using namespace std;

float Legendre(int n, float x);

int main(void){
cout<<"Enter n"<<endl;
int n;
cin>>n;
cout<<"Enter x"<<endl;
float x;
cin>>x;
cout<<Legendre(n, x)<<endl;
}

float Legendre(int n, float x){


if(n == 0){
return 1;
}
else if(n == 1){
return x;
}
return ((2*(float)n-1)/(float)n)*Legendre(n-1, x)-(((float)n-1)/(float)n)*Legendre(n-2, x);
}

Ans.

#include <iostream>
using namespace std;

int element_decider(int i, int j, int n);

int main(void){
int n;
cin>>n;

int array[n+1][n+1]{};

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


for(int j = 0; j<=n; j++){
if(i<j){
array[i][j]=0;
}
else{
array[i][j] = element_decider(i, j, n);
}
}
}

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


for(int j = 0; j<=n; j++){
cout<<array[i][j]<<" ";
}
cout<<endl;
}

int element_decider(int i, int j, int n){


int num = 1;
int digit;
for(int power = 0; power<=i; power++){
if(power == 0){
num = 1;
}
else{
num = num*11;
}
}

int digit_pos = j;
for(int i =0; i<=digit_pos; i++){
digit = num % 10;
num = num/10;
}

return digit;
}
8) In the theory of games and economic behaviour, founded by John von Neumann, certain games
can be represented by a single two-dimensional array, called the payoff matrix. Players can obtain
optimal strategies when the payoff matrix has a saddle point. A saddle point is an entry in the
matrix that is both the minimax and maximin. The minimax of a matrix is the minimum of the
column maxima, and the maximin is the maximum of the row minima. The optimal strategies are
possible when these two values are equal. Write a program that prints the minimax and the
maximin of a given matrix.

Ans.

#include <iostream>
using namespace std;

int main(void){
int row;
int column;
cin>>row;
cin>>column;
int matrix[row][column];
for(int i = 0; i<row; i++){
for(int j = 0; j<column; j++){
cin>>matrix[i][j];
}
}
int maximin;
int minimax;
int max_array[column];
int min_array[row];
for(int i = 0; i<row; i++){
min_array[i] = matrix[i][0];
for(int j = 0; j<column; j++){
min_array[i] = min(matrix[i][j], min_array[i]);
}
}
maximin = min_array[0];
for(int i = 0; i<row; i++){
maximin = max(maximin, min_array[i]);
}

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


max_array[i] = matrix[0][i];
for(int j = 0; j<row; j++){
max_array[i] = max(matrix[j][i], max_array[i]);
}
}
minimax = max_array[0];
for(int i = 0; i<column; i++){
minimax = min(minimax, max_array[i]);

}
cout<<"Maximin: "<<maximin<<endl;
cout<<"Minimax: "<<minimax<<endl;
return 0;
}

You might also like