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...
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
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’...
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void Ge
/************************************************************************************* Function Prototypes *************************************************************************************/ int ScanArray(char *fname, int myArray[], int nSize); void GenerateFromArray(void); /************************************************************************************/ /************************************************************************************/ int main(void) { /* This is the main() program. It should call the functions ScanArray() and GenerateFromArray() */ GenerateFromArray(); system("pause"); return 0; } /*************************************************************************************** Define this function to scan the elements of an array from the given data file "ArrayInp.dat". If the input file is not found, or contains invalid data, the function should return a 0 and print an error message. If the input...
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...
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...
Write a C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
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++ please 1. Write and test the function maximum that is passed an array of n...
c++ please 1. Write and test the function maximum that is passed an array of n pointers to integers and returns the maximum value among the n integers. The function must use the traveling pointer notation to traverse the array. you have to ask the user for the size of the array and ask the user to input the values in the main. The function has the following prototype. int maximum ( int *p [ ], int n);
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT