Question

Please do in C please. Edit sliding.c. Comments on code would be nice. Thank you.

Input: Seed, M, N Output: Printout of the array Example 1: Seed 123, M-10, N7 123 10 7 Initial grid: Shifted grid: 2 -1 2 2 1

Here is main.c

#include "sliding.h"
#include <malloc.h>
#include <stdlib.h>

//Prints grid in row major order.  Tries to ensure even spacing.
void print_grid(int * my_array, int rows, int cols){
        int i,j;
        for(i=0; i<rows; i++){
                for(j=0; j<cols; j++){
                        if(my_array[i*cols + j]!=-1){
                                printf(" %d ", my_array[i*cols + j]);
                        }
                        else{
                                printf("%d ", my_array[i*cols + j]);
                        }
                }
                printf("\n");
        }
}

int main(int argc, char *argv[])
{
        int seed,rows,cols;
        char buf[200];
        char garbage[2];
        //Get input from user as (seed rows columns)
        printf("Please provide input as: seed rows columns\n");
        if (NULL == fgets(buf,200,stdin)) {
                printf("\nProgram Terminated.\n");
                return 2;
        }

        //Checks input for validity
        if (3 != sscanf(buf,"%d%d%d%1s",&seed,&rows,&cols,garbage) ||
        rows < 0 || cols < 0){
                printf("Invalid Input\n");
                return 2;
        }

        //Seeds the random number generator
        srand(seed);

        //Allocate array grid
        int * my_array = (int *)malloc(rows*cols*sizeof(int));

        //Populate grid with random values -1, 1 and 2
        int i,j;
        for(i=0; i<rows; i++){
                for(j=0; j<cols; j++){
                        if(rand()%3 < 2){
                                my_array[i*cols + j] = rand()%2+1;
                        }
                        else{
                                my_array[i*cols + j] = -1;
                        }
                }
        }

        //Print initial grid
        printf("Initial grid: \n");
        print_grid(my_array, rows, cols);

        //Call sliding function
        slide_up(my_array, rows, cols);

        //Print resulting grid
        printf("Shifted grid: \n");
        print_grid(my_array, rows, cols);
        free(my_array);

        return 0;
}

Here is sliding.c:

#include "sliding.h"
/*  Slide all values of array up
*/
void slide_up(int* my_array, int rows, int cols){

    return;
}

Here is sliding.h:

#ifndef SLIDING_H
#define SLIDING_H
void slide_up(int* my_array, int rows, int cols);

#endif
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Edited sliding.c :

#include "sliding.h"
/* Slide all values of array up
*/

//the algorithm used is suggested algorithm
void slide_up(int* my_array, int rows, int cols){

   //for each column in the matrix(Note: 'j' is used for column)
   for(int j = 0 ; j < cols; j++)
   {
       //for each rows in matrix(Note: 'i' is used for rows)
       for(int i = 0; i < rows; i++)
       {
           //check if current row is not empty
           if( my_array[i*cols + j] != -1)
           {
               //finding the next target row(smallest row value (in current column) that is empty and less than current row number)
               //for that we loop from i=0 to current row
               //and check if it is empty
               for(int k = 0; k < i; k++)
               {
                   if(my_array[k*cols + j] == -1)
                   {
                       //empty, so assign the value to target row
                       my_array[k*cols + j] = my_array[i*cols + j];
                       //clearing current value
                       my_array[i*cols + j] = -1;
                       //break from the loop
                       break;

                   }
               }


           }
       }
   }

    return;
}

//==========NOTE :

//=========NO CHANGES MADE TO main.c and sliding.h

//Sample Output

Please provide input as: seed rows columns
123 10 7
Initial grid:
2 -1 2 2 1 1 2
-1 2 -1 1 1 2 -1
-1 1 2 -1 -1 2 2
2 2 -1 -1 1 2 1
2 -1 2 -1 2 2 -1
2 2 2 1 2 2 2
-1 -1 1 1 1 2 -1
1 -1 -1 1 -1 1 -1
-1 -1 2 1 1 2 -1
-1 2 2 2 2 -1 -1
Shifted grid:
2 2 2 2 1 1 2
2 1 2 1 1 2 2
2 2 2 1 1 2 1
2 2 2 1 2 2 2
1 2 1 1 2 2 -1
-1 -1 2 1 1 2 -1
-1 -1 2 2 1 2 -1
-1 -1 -1 -1 2 1 -1
-1 -1 -1 -1 -1 2 -1
-1 -1 -1 -1 -1 -1 -1

Add a comment
Know the answer?
Add Answer to:
Please do in C please. Edit sliding.c. Comments on code would be nice. Thank you. Here is main.c #include "sliding.h" #include <malloc.h> #include <stdlib.h> //Prints grid in row...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections;...

    Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections; class Grid { private boolean bombGrid[][]; private int countGrid[][]; private int numRows; private int numColumns; private int numBombs; public Grid() { this(10, 10, 25); }    public Grid(int rows, int columns) { this(rows, columns, 25); }    public Grid(int rows, int columns, int numBombs) { this.numRows = rows; this.numColumns = columns; this.numBombs = numBombs; createBombGrid(); createCountGrid(); }    public int getNumRows() { return numRows;...

  • Using java : In this exercise, you need to implement a class that encapsulate a Grid. A grid is a useful concept in crea...

    Using java : In this exercise, you need to implement a class that encapsulate a Grid. A grid is a useful concept in creating board-game applications. Later we will use this class to create a board game. A grid is a two-dimensional matrix (see example below) with the same number of rows and columns. You can create a grid of size 8, for example, it’s an 8x8 grid. There are 64 cells in this grid. A cell in the grid...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • C Programming #include <stdio.h> #include <stdlib.h> #include <time.h> // These defines do a text repl...

    C Programming #include <stdio.h> #include <stdlib.h> #include <time.h> // These defines do a text replacement // everytime the string 'ROWS' and 'COLUMNS' // are found in this specific source file. // You can play with these values. #define ROWS 5 #define COLUMNS 5 /* ============= Tutorial on Graph format ============ You are given a randomly generated graph that looks of the form: 0 0 1 1 1 1 0 0 1 1 0 1 0 1 1 1 0 0...

  • The objective is to use each row of the first column as a starting point to...

    The objective is to use each row of the first column as a starting point to come up with a sum of absolute value of differences . For each row, a sum will be calculated, therefore, 5 sums must be calculated. Each sum will be placed into the array, sumList[ ]. #include <stdio.h> #include <stdlib.h> #include <math.h> #define ROWS 5 #define COLS 6 void calcSums(int topog[ROWS][COLS], int sumList[ROWS] ); int main() { int topography[ROWS][COLS] = { { 3011, 2800, 2852,...

  • Please help i need a C++ version of this code and keep getting java versions. Please...

    Please help i need a C++ version of this code and keep getting java versions. Please C++ only Purpose: This lab will give you experience harnessing an existing backtracking algorithm for the eight queens problem, and seeing its results displayed on your console window (that is, the location of standard output). Lab A mostly complete version of the eight queens problem has been provided for you to download. This version has the class Queens nearly completed. You are to provide...

  • C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <...

    C++ how can I fix these errors this is my code main.cpp #include "SpecialArray.h" #include <iostream> #include <fstream> #include <string> using namespace std; int measureElementsPerLine(ifstream& inFile) {    // Add your code here.    string line;    getline(inFile, line);    int sp = 0;    for (int i = 0; i < line.size(); i++)    {        if (line[i] == ' ')            sp++;    }    sp++;    return sp; } int measureLines(ifstream& inFile) {    // Add your code here.    string line;    int n = 0;    while (!inFile.eof())    {        getline(inFile,...

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

  • Can I get help with adding the following to this code please? 1. When buying multiple...

    Can I get help with adding the following to this code please? 1. When buying multiple tickets, if one of the seats selected is already taken, give a message like "Sorry, one or more of the seats selected is already taken." and prompt the user to select the seats all over. (Or if possible to make it suggest a location where the seats are together that the user can select instead) 2. Print the total cost after all of the...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT