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

I am working in C language and need this function to work, please help Function:

ID: 3758148 • Letter: I

Question

 I am working in C language and need this function to work, please help Function:  gravity  Description:  "compacts" the matrix/image downward according to the                 following rules:                 - The character '.' represents Air                 - All other characters are assumed to be "heavier than air" --                         i.e., they will fall when the force of gravity is                         applied.                         Let's call them "blocks"                 - All blocks fall as far as possible as you might expect...                     Example:          Before Gravity:             .^..8.             .^##..             #....#             #.#@.$             &###.#             ..##*.          After Gravity:             ......             ......             .^##..             #^#@.#             ####8$             &##### void gravity(char m[N][N]) 

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 10
void gravity(char m[N][N])
{
int i,j,x,g, row, col;
char temp;
char a;
srand((unsigned)time(NULL));
for(j = 0; j<N; j++)
{
for(i = 0; i <N; i++)
{
x= rand()%4;
if(x<=0)
{
a = '.';
m[i][j] = a;
}
else if(x == 1)
{
a = '#';
m[i][j] = a;
}
else if(x == 2)
{
a = '@';
m[i][j] = a;
}
else if(x == 3)
{
a = '&';
m[i][j] = a;
}
}
}
printf("Before gravity ");
for(i = 0; i < N; i ++)
{
for(j = 0; j <N; j++)
{
printf(" %c ", m[i][j]);
}
printf(" ");
}
  
for(col = 0; col < N; col++) /*For each column*/
{
for(row = N-2; row >= 0; row--) /*Starting from last but one row till the first row.*/
{
for(i = N-2; i >= N-row-2; i--)
if(m[i+1][col] == '.') /*If . is below any character, pop it up.*/
{
temp = m[i][col];
m[i][col] = m[i+1][col];
m[i+1][col] = temp;
}
}
}   

printf("After gravity ");
for(i = 0; i < N; i ++)
{
for(j = 0; j <N; j++)
{
printf(" %c ", m[i][j]);
}
printf(" ");
}
}

int main()
{
char m[N][N];
gravity(m);
}

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