Array Classwork
Array Classwork
Day5_CPP_Arrays_Class
Attempt : 1
Total Mark : 50
Marks Obtained : 48
Section 1 : Coding
1. Create a dynamic integer array and print the sum over the array of each
element * (array element index + 1)
Answer
// You are using GCC
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int size1,i;
int sum=0;
cin>>size1;
int arr[size1];
for(i=0;i<size1;i++)
{
cin>>arr[i];
}
for(int j=0;j<size1;j++)
{
sum=sum+(arr[j]*(j+1));
}
cout<<sum<<endl;
}
Mukesh and his friends have set out on a vacation to Coorg. They have
booked accommodation in a resort and the resort authorities organize
Campfires every night as a part of their daily activities. Mukesh
volunteered himself for an activity called the "Adjacent Stick Game" where
sticks of different lengths will be placed in a line and Mukesh needs to
remove a stick from each adjacent pair of sticks. He then has to form a
bigger stick by combining all the remaining sticks.
Mukesh needs to know the smallest length of the bigger stick so formed
and needs your help to compute the same. Given the number of sticks N
and the lengths of each of the sticks, write a program to find the smallest
length of the bigger stick that is formed.
Answer
// You are using GCC
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int i,sum,n;
cin>>n;
sum=0;
int a[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(int j=0;j<n;j=j+2)
{
if(a[j]<a[j+1])
{
sum=sum+a[j];
}
else
{
sum=sum+a[j+1];
}
}
cout<<sum;
Answer
// You are using GCC
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n,s1=0,s2=0,s3=0;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for(int i=0;i<n;i++)
{
switch(a[i])
{
case 1:
s1++;
break;
case 2:
s2++;
break;
case 3:
s3++;
break;
}
}
if(s1>s2 && s1>s3)
{
cout<<"1";
}
else if(s2>s1 && s2>s3)
{
cout<<"2";
}
else if(s3>s2 && s3>s1)
{
cout<<"3";
}
else
{
cout<<"1";
}
}
4. Mid Aged
The Pan Am 73 flight from Bombay to New York en route to Karachi and
Frankfurt was hijacked by a few Palestinian terrorists at the Karachi
International Airport. The senior flight purser Neerja Banhot withered her
fear and helped evacuate the passengers on board.
Neerja very well knew that she would not be able to evacuate all
passengers dodging the hijackers. So she wanted to hand over the
responsibility of evacuating the senior citizens(above 60 years of age) and
children(below 18 years of age) in the flight to the mid-aged passengers
seated in the diagonals
Given n the number of rows of seats and the number of seats in a row and
the ages of passengers in each seat can you find the number of mid-aged
passengers seated in the diagonals.
Answer
// You are using GCC
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j,n,s=0;
cin>>n;
int a[n][n];
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(i==j)
{
if(a[i][j]>=18 && a[i][j]<=60)
{
s++;
}
}
}
}cout<<s;
}
5. Number of White
Cells
Nurikabe logical game (sometimes called Islands in the Stream) is a binary
determination puzzle. The puzzle is played on a typically rectangular grid
of cells, some of which contain numbers. You must decide for each cell if it
is white or black (by clicking on them) according to the following rules:
• All of the black cells must be connected.
• Each numbered cell must be part of a white island of connected white
cells.
• Each island must have the same number of white cells as the number it
contains (including the numbered cell).
• Two islands may not be connected.
• There cannot be any 2x2 blocks of black cells.
Unnumbered cells start out grey and cycle through white and black when
clicked. Initially numbered cells are white in color.
Problem Statement:
Write a program to find the number of white cells in the final configuration
of the board, given a valid initial configuration. Below figure is the sample
valid initial configuration.
Answer
// You are using GCC
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n,s=0;
cin>>n;
int a[n][n];
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cin>>a[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]!=20)
{
s=s+a[i][j];
}
}
}cout<<s;
}