You are given a 9*9 grid as shown in the figure below. You can determine the color
of each square from the grid. The number of each square is also shown below. Write a C++ program for
this below given grid. The user is given two options to select from.
Part A: When the user selects 1st option, the user will enter number of square in this grid (any
number from 1-81). Your program will determine the color of the square.
Part B: When the user enters 2nd option, the user will enter two numbers from this grid. Your
program will determine if the two squares entered in this 9*9 grid have same color or not.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int option,num1,num2,temp1,temp2;
cout << "Choose option 1 or 2\n";
cin >> option ;
if(option==1)
{
cout << "Enter any number between 1 and 81\n";
cin >> num1;
if(((int)ceil((float)num1/(float)9))%2 == 1)
{
if((num1%9)%3 == 0)
cout << "red";
if((num1%9)%3 == 1)
cout <<"gray";
if((num1%9)%3 == 2)
cout << "blue";
}
else
{
if((num1%9)%3 == 0)
cout << "gray";
if((num1%9)%3 == 1)
cout <<"blue";
if((num1%9)%3 == 2)
cout << "red";
}
}
else if(option==2)
{
cout << "Enter any two numbers between 1 and 81\n";
cin >> num1>>num2;
if(((int)ceil((float)num1/(float)9))%2 == 1)
{
if((num1%9)%3 == 0)
temp1 = 0;
else if((num1%9)%3 == 1)
temp1 = 1;
else if((num1%9)%3 == 2)
temp1 = 2;
}
else
{
if((num1%9)%3 == 0)
temp1 = 1;
else if((num1%9)%3 == 1)
temp1 = 2;
else if((num1%9)%3 == 2)
temp1 = 0;
}
if(((int)ceil((float)num2/(float)9))%2 == 1)
{
if((num2%9)%3 == 0)
temp2 = 0;
else if((num2%9)%3 == 1)
temp2 =1;
else if((num2%9)%3 == 2)
temp2 = 2;
}
else
{
if((num2%9)%3 == 0)
temp2 = 1;
else if((num2%9)%3 == 1)
temp2 =2;
else if((num2%9)%3 == 2)
temp2 = 0;
}
if(temp1==temp2)
cout<<"Yes the colors match";
else
cout<<"No the colors don't match";
}
return 0;
}
Comments
Leave a comment