6 December

Exercises with arrays

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

void insert(int a[], int size, int i,
int new_element)
{
// shift everything right
for(int j = size-1; j > i; j--)
{
a[j] = a[j-1];
}
// put new element
a[i] = new_element;
}

// Return the position of the element "goal"
// in the array "a", OR -1 if not found.
int linear_search(int a[], int size,
int goal)
{
for(int i = 0; i < size; i++)
{
if(a[i] == goal)
{
return i;
}
}

return -1;
}

int find_smallest(int a[], int size)
{
int smallest;
if(a[0] < a[1])
{
smallest = a[0];
}
else
{
smallest = a[1];
}
for(int i = 2; i < size; i++)
{
if(a[i] < smallest)
{
smallest = a[i];
}
}
return smallest;
}

void print_array(int a[], int size)
{
for(int i = 0; i < size; i++)
{
printf("%3d", a[i]);
}
printf("\n");
}

int main99()
{
srand(time(NULL));
const int size = 8;
int a[size];
// init randomly
for(int i = 0; i < size; i++)
{
a[i] = rand()%10 + 1; // 1 to 20
}
print_array(a,size);
// search for 10.
int j = linear_search(a, size, 10);
if(j < 0) { // didnt find it.
printf("Goal not found.\n");
}
else {
printf("Goal found at position %d\n",
j);
}
insert(a, size, 3, 99);
print_array(a,size);
int s = find_smallest(a, size);
printf("Smallest element is %d\n",
s);
return 0;
}

More assistance on project

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

/* Top-level constants */
const int M = 8;
const int N = 9;

/* The function below takes the grid and a coordinate (i,j) as
parameters, and determines whether it's possible to move to that
position. It should check whether the position is off the edge of
the board. If it's not it should also check that the position is
still available (contains the '.' character). */
bool can_i_go_to (char grid[M][N], int i, int j)
{
if(i < 0 || j < 0 || i >= M || j >= N)
return false; // out of bounds
return grid[i][j] == '.';
}

/* Determines whether we are completely stuck at position (i,j) on the
grid. We are stuck if it's not possible to move left, right, up, OR
down. (Hint: use the function above to check where you can go.
Directions left/right are obtained by j-1 or j+1; up/down are i-1
or i+1. */
bool am_i_stuck(char grid[M][N], int i, int j)
{
if(can_i_go_to(grid, i, j+1)) // right
{
return false;
}

//grid[i][j+1] // right
//grid[i+1][j] // down
//grid[i][j-1] // left
//grid[i-1][j] // up
return false;
}


/* Initialize the grid to contain all period (dot) characters. */
void initialize_grid (char grid[M][N]);

/* Output the grid onto the screen using printf. */
void output_grid (char grid[M][N])
{
for(int i = 0; i < M; i++)
{
for(int j = 0; j < N; j++)
{
printf("%c", grid[i][j]);
}
printf("\n");
}

}

/* Choose a random direction that is a valid move. The coordinates i,j
are reference parameters so that this function can modify them to
implement the move. You will have to use a loop -- if rand()
produces a direction in which we cannot move, just try again. NOTE:
This function can safely assume that we are NOT STUCK, and so when
you call it, make sure you check that first (or else it might be an
infinite loop). */
void random_direction (char grid[M][N], int& i, int& j)
{
int dir = rand()%4;
switch(dir)
{
case 0: // right
if(can_i_go_to(grid, i, j+1))
{
j++;
}
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
}

/* The rest of the work -- the main loop that counts from 'A' up to
'Z' -- can happen in main. */
int main()
{
char grid[M][N];
srand(time(NULL));
initialize_grid(grid);
// some experimentation
int i = 0;
int j = 0;
char letter = 'A';
grid[i][j] = letter;
while(letter < 'Z' && !am_i_stuck(grid,i,j))
{
random_direction(grid,i,j);
letter++;
grid[i][j] = letter;
}
output_grid(grid);
return 0;
}

void initialize_grid(char grid[M][N])
{
int j; int i;
for (i = 0; i < M ; i++)
{
for (j = 0; j < N; j++)
{
grid [i] [j]= ('.');
}
}
// Fill grid with . character
}

©2011 Christopher League · some rights reserved · CC by-sa