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

solve and run this program Problem 2: In the following the C function named cc t

ID: 3683007 • Letter: S

Question


solve and run this program

Problem 2: In the following the C function named cc takes as its first argument a two dimensional character array named arr with NR rows and NC columns where NR and NC are symbolic constants. The second argument to the function is a two dimensional integer array named locations with 2 columns and NCC rows where NCC is a symbolic constant equal to the number of elements in the array arr The third argument to the function is a single character variable named ch The function stores in the integer array named locations the locations (as row and column index values) of all the occurrences of the character ch in the array arr. In the example below the finnction sets row 0 of locations to the values o and 1 in columns 0 and 1 respec The function returns void, but uses its last argument to retum the number of times the churacter searched for was found. The character array data is read from the file data dat The character to Hearch for is read from the keyboard follows: An example run with the program for this problem

Explanation / Answer

int const NR = 1;
int const NC = 1;       //Define the constants
int const NCC = 1;

void cc(char arr[NR][NC],char locations[NCC][2],char ch,int** count)
{
int count1=0;
for(int i=0;i<NR;i++)
   for(int j=0;j<NC;j++)
   {
       if(arr[i][j]==ch)
       {
           locations[i][j]=ch;
           count1++;
       }
   }
   *count = (int*) malloc(sizeof(int));
(*(*count)) = count1;
}


int main()
{


FILE *ptr_file;
char buf[1000];

ptr_file =fopen("input.txt","r");
if (!ptr_file)
        return 1;

while (fgets(buf,1000, ptr_file)!=NULL)
   //handle how the data from the file is to be stored in the array. Details for the same aren't mentioned.
   char ch;
   char locations[NCC][2];
   char arr[NR][NC];
   printf("Enter the character to be searched: ");
   scanf("%c",ch);
  
   int *count=NULL;
   cc(arr,locations,ch,&count);
  

}