Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am writing a program in C that will take out a row and column in a board if th

ID: 3574946 • Letter: I

Question

I am writing a program in C that will take out a row and column in a board if the command entered is entered. so the userinput can be

r r 4 for removing the fourth row

r c 2 for removing the second column

I have having a problem passing the pointers correct and it won't compile correctly. if anyone can take out the wrong pointers, and function calls I would be much obliged.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
//#include "paint.h"

bool integer_check(float num, char* arg)
{
   if(num<0.00)
   {
       printf("The number of %s is less than 1. ",arg);
       return false;
   }  
   if(num-(int)num == 0)
   {
       return true;
   }
   else
   {
       printf("The number of %s is not an integer. ",arg);
       return false;
   }
}

/*
void row_erase(int rowerase, char** board, int num_rows, int num_cols){
int row = 0;
free(board[rowerase]);
//moove rows to the left
while(row<num_rows)
{
board[row] = board[row+1];
row++;
}
}

*/
void col_erase(int rowerase, char** board, int num_rows, int num_cols){
//int col = 0;
printf("Punk");
}

void read_args(int argc, char** argv, int* num_rows, int* num_cols){
   float rows, cols;
   if(argc == 1){
      
       *num_rows = 10;
       *num_cols = 10;

   }

   else
   {
       rows = atoi(argv[1]);
       cols = atoi(argv[2]);
       if(!(integer_check(rows,"rows")) || !(integer_check(cols,"columns")))
       {
           *num_rows = 10;
           *num_cols = 10;
           printf("Making default board of %d X %d. ",*num_rows,*num_cols);
       }
       else
       {
           *num_rows = atoi(argv[1]);
           *num_cols = atoi(argv[2]);  
       }
   }
}

bool is_valid_formatting(int num_args_read, int num_args_needed){
  

   bool is_valid = true;
   if(num_args_read != num_args_needed){
       is_valid = false;
   }
  
   return is_valid;
}


bool write_function(char* userInput)
{
   char command[50];
   int num_args_read;
   int a,b,c,d;
  
   num_args_read = sscanf(userInput," %s %d %d %d %d",command, &a, &b, &c, &d);
  

   if(!(is_valid_formatting(num_args_read,5) == true))
   {
       printf("Improper draw command. ");
       return false;
   }  
   else
   {
       printf("Your input was %c %d %d %d %d with %d arguments ", command[0], a,b,c,d,num_args_read);
       return true;
}
}

/*
void save_function(char* userInput)
{
   int num_args_read;
   char command[10], filename[50];
  
   num_args_read = sscanf(userInput,"%s %s", command, filename);
  
}

void copy_to_file(char** board, int num_rows, int num_cols, char* filename)
{
FILE *fp;
fp = fopen(filename.txt, "w");
int i =0;
int j = 0;
for (i = (num_rows - 1); i >= 0; --i) {
fprintf("%d ", i);
for (j = 0; j < num_cols; ++j) {
fprintf("%c ", board[i][j]);
}
fprintf(" ");
}

fclose(f);

}//end coppy to board*/

char get_valid_input(char* userInput, char** board, int num_rows, int num_cols)
{   
   userInput[0] = tolower(userInput[0]);
   
    if(userInput[0] == 'q')
    {
    //   quit_paint(10);
   }
    else if(userInput[0] == 'h')
      {  
    //   print_help();
       }
    else if(userInput[0] == 'w')
        write_function(userInput);
    /*else if(userInput[0] == 'e')
        return erase_function(userInput);
    else if(userInput[0]== 'r')
       return resize_function(userInput);
   else if(userInput[0] == 'a')
       return add_function(userInput);
*/
   else if(userInput[0] == 'd'){
      
    int number = atoi(&userInput[4]);

   if(userInput[2] == 'c' ){
   col_erase(number,board,num_rows,num_cols);
   }
   //else if(userInput[2]== 'r'){      
   // row_erase(number, char**board, int num_rows, int num_cols);
  
   //   }
   }  


/*   else if(userInput[0] == 's')
       save_function(userInput);
   else if(userInput[0]== 'l')
       return load(userInput);
   */
    else
    {
        printf("Unrecognized command. Type h for help. ");
   }
  
   return userInput[0];
}

int main(int argc, char** argv)
{
   int num_rows=0, num_cols=0;
   char** board;
   char* command;
  
read_args(argc, argv, &num_rows, &num_cols);
create_board(&board, num_rows, num_cols);
command = (char*)malloc(10*sizeof(char));
char c;
  
   do
   {
       print_board(board, num_rows, num_cols);
       printf("Enter your command: ");
    //fgets(command, 100, stdin);
scanf("%s",command);//modified
c=get_valid_input(command,board, num_rows, num_cols);
}
while( c!= 'q');

   destroy_board(board,num_rows);
   return 0;   
}
  

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
//#include "paint.h"

bool integer_check(float num, char* arg)
{
if(num<0.00)
{
printf("The number of %s is less than 1. ",arg);
return false;
}
if(num-(int)num == 0)
{
return true;
}
else
{
printf("The number of %s is not an integer. ",arg);
return false;
}
}

void row_erase(int rowerase, char** board, int num_rows, int num_cols){
int row = 0;
free(board[rowerase]);
//moove rows to the left
while(row<num_rows)
{
board[row] = board[row+1];
row++;
}
}

void col_erase(int rowerase, char** board, int num_rows, int num_cols){
//int col = 0;
printf("Punk");
}

void read_args(int argc, char** argv, int* num_rows, int* num_cols){
float rows, cols;
if(argc == 1){
  
*num_rows = 10;
*num_cols = 10;

}

else
{
rows = atoi(argv[1]);
cols = atoi(argv[2]);
if(!(integer_check(rows,"rows")) || !(integer_check(cols,"columns")))
{
*num_rows = 10;
*num_cols = 10;
printf("Making default board of %d X %d. ",*num_rows,*num_cols);
}
else
{
*num_rows = atoi(argv[1]);
*num_cols = atoi(argv[2]);
}
}
}

bool is_valid_formatting(int num_args_read, int num_args_needed){
  

bool is_valid = true;
if(num_args_read != num_args_needed){
is_valid = false;
}
  
return is_valid;
}


bool write_function(char* userInput)
{
char command[50];
int num_args_read;
int a,b,c,d;
  
num_args_read = sscanf(userInput," %s %d %d %d %d",command, &a, &b, &c, &d);
  

if(!(is_valid_formatting(num_args_read,5) == true))
{
printf("Improper draw command. ");
return false;
}
else
{
printf("Your input was %c %d %d %d %d with %d arguments ", command[0], a,b,c,d,num_args_read);
return true;
}
}


void save_function(char* userInput)
{
int num_args_read;
char command[10], filename[50];
  
num_args_read = sscanf(userInput,"%s %s", command, filename);
  
}

void copy_to_file(char** board, int num_rows, int num_cols, char* filename)
{
FILE *fp;
fp = fopen("file.txt", "w");
int i =0;
int j = 0;
for (i = (num_rows - 1); i >= 0; --i) {
printf("%d ", i);
for (j = 0; j < num_cols; ++j) {
printf("%c ", board[i][j]);
}
printf(" ");
}

fclose(fp);

}//end coppy to board

char get_valid_input(char* userInput, char** board, int num_rows, int num_cols)
{   
userInput[0] = tolower(userInput[0]);

if(userInput[0] == 'q')
{
// quit_paint(10);
}
else if(userInput[0] == 'h')
{
// print_help();
}
else if(userInput[0] == 'w')
write_function(userInput);
/*else if(userInput[0] == 'e')
return erase_function(userInput);
else if(userInput[0]== 'r')
return resize_function(userInput);
else if(userInput[0] == 'a')
return add_function(userInput);
*/
else if(userInput[0] == 'd'){
  
int number = atoi(&userInput[4]);

if(userInput[2] == 'c' ){
col_erase(number,board,num_rows,num_cols);
}
else if(userInput[2]== 'r'){
row_erase(number,board, num_rows, num_cols);
  
}
}


/* else if(userInput[0] == 's')
save_function(userInput);
else if(userInput[0]== 'l')
return load(userInput);
*/
else
{
printf("Unrecognized command. Type h for help. ");
}
  
return userInput[0];
}

int main(int argc, char** argv)
{
int num_rows=0, num_cols=0;
char** board;
char* command;
  
read_args(argc, argv, &num_rows, &num_cols);
create_board(&board, num_rows, num_cols);
command = (char*)malloc(10*sizeof(char));
char c;
  
do
{
print_board(board, num_rows, num_cols);
printf("Enter your command: ");
//fgets(command, 100, stdin);
scanf("%s",command);//modified
c=get_valid_input(command,board, num_rows, num_cols);
}
while( c!= 'q');

destroy_board(board,num_rows);
return 0;   
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote