Question

Your C program will do the following : Must use at least 2 function prototypes &...

Your C program will do the following :

Must use at least 2 function prototypes & definitions . You can also use repetitions , control structures . You re not allowed any type of global arrays, or global variables. You are only allowed to use 2 dimensional arrays.

1. In your main program, create a array of size 7 X 7.

2. Create a function that accepts the empty array. The function will initiate the to zero. Then, the function will populate the array with random number of 0’s and 1’s only. Use a random number generator to generate the 0’s and 1’s.

3. From main Display the 2 d array.

4. From main , pass the populated array to another function.

The purpose of the to do the following : a. Asks for two integer indices i and j (less or equal to n) or -1 .

b. Return the number of nonzero elements of the array that are adjacent to the i-th and j-th element of the array

c. repeat steps a and b until -1 is entered to exit the function.

5. Repeat steps 2 – 4 until the user terminates the program by entering N/n .  

  

You need to use the following libraries in order to generate the random number stdlib , stdio, and time libraries . you also need the srand(time(0)) in order to get a different random number every time you generate a number.

Finally , you need the command that generates the number and stores it in a variable .

• i-th row and j-column must be integers between 1 and 7 or -1 to terminate. Otherwise an error should be displayed.

• The program must be running until the user decides to terminated it by entering n.  

Homework Answers

Answer #1

Here is the answer for your question in C Programming Language.

Kindly upvote if you find the answer helpful.

####################################################################

CODE :

#include<stdio.h>
#include<stdlib.h>
#include<time.h>


void populateArray(int[][7]);
int nonZeroElements(int[][7]);

void main(){
   //Create an array of size 7 X 7
   int array[7][7],i,j,nonZeros = 0;
   char ch = "y",ignore;
   populateArray(array);
   printf("Generated 2D array with 0's and 1's: \n");
   printf("====================================\n");
   while(ch != 'n' && ch != 'N'){
       for(i = 0;i<7;i++){
           for(j = 0;j<7;j++)
               printf("%d ",array[i][j]);
           printf("\n");
       }
       nonZeros = nonZeroElements(array);
       while(nonZeros != -1){
           printf("Number of non Zeros are : ");
           printf("%d \n",nonZeros);
           nonZeros = nonZeroElements(array);
       }  
       printf("\nDo you want to continue[Y/N]? ");
       scanf("%c",&ignore);
       scanf("%c",&ch);                  
   }
  
}
void populateArray(int array[][7]){
   srand(time(0));
   int i,j;
   //Initiate the array to 0
   for(i = 0;i<7;i++)
       for(j = 0;j<7;j++){
           array[i][j] = 0;
       }
   //Populate the array with 0's and 1's
   for(i = 0;i<7;i++)
       for(j = 0;j<7;j++){
           array[i][j] = (rand() % 2);
       }
}
int nonZeroElements(int array[][7]){
   int i,j,k,m,h,count = 0;

   printf("Enter i and j(1 to 7) indices values(-1 to stop): ");
   scanf("%d%d",&i,&j);
   if(i == -1 || j == -1)
       return -1;
   if((i <= 0 || i > 7) || (j <=0 && j > 7)){
       printf("Invalid index error");
   }else{
       count = 0;
       i = i-1;
       j = j-1;
       if(i == 0){
           if(j >= 0 && j<6){
               if(j == 0)
                   h = j;  
               else
                   h = (j-1);
               for(k = i;k<=i+1;k++){
                   for(m = h;m<=(j+1);m++){
                       if(k != i || m != j){
                           if(array[k][m] == 1)
                               count++;
                       }
                   }
               }
           }else if(j == 6){
               for(k = i;k<=(i+1);k++){
                   for(m = j;m>=(j-1);m--){
                       if(k != i || m != j){
                           if(array[k][m] == 1)
                               count++;
                       }
                   }
               }
           }
       }else if(i == 6){
           if(j >= 0 && j<6){
               if(j == 0)
                   h = j;  
               else
                   h = (j-1);
               for(k = i;k>=(i-1);k--){
                   for(m =h;m<=(j+1);m++){
                       if(k != i || m != j){
                           if(array[k][m] == 1)
                               count++;
                       }
                   }
               }
           }else if(j == 6){
               for(k = i;k>=(i-1);k--){
                   for(m = j;m>=(j-1);m--){
                       if(k != i || m != j){
                           if(array[k][m] == 1)
                               count++;
                       }
                   }
               }
           }
       }else{          
           for(k = (i-1);k<=(i+1);k++){
               for(m = (j-1);m<=(j+1);m++){                  
                   if(k != i || m != j){
                       if(array[k][m] == 1)
                           count++;
                   }
               }
           }
       }
   }
   return count;
}

#######################################################

SCREENSHOTS :

Please see the screenshots of the code below for the indentations of the code.

####################################################################

OUTPUT :

Any doubts regarding this can be explained with pleasure :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
A) write a program the computes nx and store the result into y You can use...
A) write a program the computes nx and store the result into y You can use y = Math.pow( Mantissa, exponent) Requirements: Besides main() your program must have one method with two parameters, one double and one int n and x are positive numbers read from the keyboard if the user makes an entry that does not meet this criteria, the user must be given to opportunity retry the entry the user must be able to quit prior to entering...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
Please write it in c# program please and if possible no arrays only loops Loop Introduction...
Please write it in c# program please and if possible no arrays only loops Loop Introduction Assignment Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches.   Your function will convert the weight and height into Body Mass Index (BMI). The formula for converting weight into...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an...
Arrays, loops, functions: Lotto Element Repeated Function Write a function that that takes as parameters an array of ints, an int value named element, and an int value named end. Return a bool based on whether the element appears in the array starting from index 0 and up to but not including the end index. Generate Random Array Write a function that takes as parameters an array of integers and another integer for the size of the array. Create a...
Coffee Order Structure Assignment Write a program that uses a structure for a coffee order. A...
Coffee Order Structure Assignment Write a program that uses a structure for a coffee order. A coffee order consists of a double price, a string for flavor, and characters for cream (Y/N), sugar (Y/N), and coffee size (S/M/L). Your main() function will call an order function that will ask the user for the flavor, size and whether or not they want cream or sugar. Your main() function will then create a coffee order structure and assign all of the values...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF...
WRITE C++ PROGRAM FOR 1,2,3,4 PARTS of question, DO ADD COOMENTS AND DISPLAY THE OUTPUT OF A RUNNING COMPILER QUESTION: 1) Fibonacci sequence is a sequence in which every number after the first two is the sum of the two preceding ones. Write a C++ program that takes a number n from user and populate an array with first n Fibonacci numbers. For example: For n=10 Fibonacci Numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 2): Write...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using...
Please do it in Python Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Take the array and its size as input params and return a bool. Print out a message "Entering <function_name>" as the first statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT