All three programs expect a file as input. If the file is not present, each prog
ID: 3551046 • Letter: A
Question
All three programs expect a file as input. If the file is not present, each program should exit silently with an exit code of 1 (i.e. main() returns 1)
EXAMPLE input and output files are available in the directories:
Summary: test if a board is a "sudoku" board.
Details:
Write a program that will read a "solved" (i.e. fully revealed) sudoku board and determine if the board has the appropriate placement of digits 1 through 9 in each row, column, and box.
The input file is called "sudokuboard.txt" and the program should output to the terminal either "valid" or "invalid" and nothing else.
The file format is 9 characters per line, representing each row, as in:
Note: this is not a valid sudoku board.
Example boards will be provided in the format expected.
Explanation / Answer
#include<iostream>
#include<fstream>
#include<math.h>
using namespace std;
int main()
{
ifstream myfile;
myfile.open("example.txt");
int j;
int data[9];
for (int i=0;i<9;i++)
{
myfile>>data[i];
}
int data1[9][9];
int k,l,m;
for (k=0;k<9;k++)
{
m=data[k];
for (j=0;j<9;j++)
{
l=(m/(pow(10,8-j)));
data1[k][j]=l%10;
}
}
int flag=0;
//checking row wise
for(j=0;j<9;j++)
{
for (k=0;k<9;k++)
{
for(l=k+1;l<9;l++)
{
if(data1[j][k]==data1[j][l])
{
flag=1;
}
}
}
}
//checking column wise
for(j=0;j<9;j++)
{
for (k=0;k<9;k++)
{
for(l=k+1;l<9;l++)
{
if(data1[k][j]==data1[l][j])
{
flag=1;
}
}
}
}
if(flag==1)
cout<<"invalid"<<endl;
else
cout<<"valid"<<endl;
myfile.close();
}