# CIS 1057 Programming Project 7 This project is arrays. You will need to write a series of functions

CIS 1057 Programming Project 7

This project is arrays. You will need to write a series of functions to perform operations on arrays. Only use one main function for this exercise and call functions to complete the individual tasks. All variables must be declared locally (inside a function, not global or outside a function). Define three constants as (copy and paste):

#define COUNT 10

### Save your time - order a paper!

Get your paper written from scratch within the tight deadline. Our service is a reliable solution to all your troubles. Place an order on any task and we will take care of it. You won’t have to worry about the quality and deadlines

Order Paper Now

#define COUNT_COLS 5

#define COUNT_ROWS 3

Declare arrays with 10 elements as follows (copy and paste):

int array1[COUNT] = {5, 9, 3, 1, 7, 2, 8, 0, 4, 6};

int array2[COUNT] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

int array3[COUNT];

Write the following functions:

Write a function to print an array to screen. Test this on array1 and array2.

Write a function to sum the elements in an array. Test this on array1 and array2.

Write a function to compare two arrays, element-by-element, to see if they contain the same integers in exactly the same order. Test this on array1 and array2. They should not be equal unless sorted.

Write a function to test if an array is sorted.    Test this on array1 and array2. array1 should return 0 and array2 should return 1.

Write a function to sort an array using a bubble sort. Test this on array1. array1 should be sorted after the function call. Use the previous function to check if array1 is now sorted.

Write a function to perform a binary search of an array to find an integer value. If the value is found, return its index, else return -1.

Declare a 2D matrix as:

 int matrix[COUNT_ROWS][COUNT_COLS] = {{1, 2, 3, 4, 5}, {2, 4, 6, 8, 10}, {3, 6, 9, 12, 15}};

Write the following functions:

Write a function to print the matrix to screen.

Write a function to sum the elements in a row.

Write a function to sum the elements in a column.

Write a function to sum all of the elements in a matrix.

You can use any of the code from the lecture examples. This should make this much easier. Please write the function to print an array first and write the other functions in order. This will make the project easier. The code is in the lecture examples. Just copy and paste into a function. If you can get this working, you will better understand the rest of the functions. All of your output should print clearly and neatly to the screen. You should include comments and meaningful variable names.

Check if an array is sorted

An array is sorted if for each element pair     ≤          . To use a binary search, you need to ensure that an array is sorted.

The Bubble Sort

The bubble sort sorts an array by starting at the last array element and comparing it to the second to the last element. If the last element is less, the elements are swapped. You can copy the swap function from the Lecture 7 code. Start by writing the inner loop to find the first min element and bubble it up to the zero index position. The algorithm below shows the inner loop.

// From the last element (n-1) to the 1 index element

// If the compared j element is less than j-1

// Swap them

Next, add the outer loop to finish the sort on the array. Remember that after each iteration, the array will move the minimal elements to their proper position and that i is the boundary for the integers that are already sorted. The loop repeatedly pulls the minimal values to their correct positions in the sorted array.

// Bubble sort construction 2 – find the rest of min

// From the i = 0 index to the last (n-1) index

// From the j = last element (n-1) to the i+1 index element

// If the compared j element is less than j-1

// Swap them

The Binary Search

A binary search is much more efficient than a selection search. In the worst case, a selection sort will have to compare the query value to all other values in the array. For an array with 256 elements, the worst case is 256 comparisons. This is referred to as an           complexity because the number of comparisons grows linearly with the data. This worst case occurs if the query value matches the last element in the array or if the query value is not in the array. A binary search repeatedly cuts the elements in the array in half on a middle element, calculated as (end-begin)/2, where end is the last element under consideration and begin is the first. Because the data in the array are sorted, there are 3 cases: the query value is equal to the middle value (match found), the query value is less than the middle value (check values before mid) or the query value is greater than the middle value (check values after mid). In the worst case this search will require log comparisons or only 8 comparisons for an array containing 256 elements (log256 = 8). This is a log       complexity. First start by writing a function that checks to see if an array is sorted. Then write a function that checks, and if the array is not sorted, sort it. The last step is to code the binary search as described below.

// While begin is less than end

// If query value is greater than mid value

// begin is mid+1

// If query value is less than mid value

// end is mid-1

// If query value is equal to mid value

// Match found – return index

// mid = (begin-end)/2

.

.

.

.

.

.

.

.

.

.

.

.

Please read carefully. The program is in C and i just need the 2D matrix part code;

Declare a 2D matrix as:

 int matrix[COUNT_ROWS][COUNT_COLS] = {{1, 2, 3, 4, 5}, {2, 4, 6, 8, 10}, {3, 6, 9, 12, 15}};

Write the following functions:

Write a function to print the matrix to screen.

Write a function to sum the elements in a row.

Write a function to sum the elements in a column.

Write a function to sum all of the elements in a matrix.

.

.

.

I will post the first code line so you can follow from there.

Here is the code for 1-D arrays:

#include

#define COUNT 10
#define COUNT_COLS
#define COUNT_ROWS 3
#define TRUE 1
#define FALSE 0
//Write a function to print an array to screen. Test this on array1 and array2.
void print_array(int arr[]);
int sum_array(int arr[]);
void compare_arrays(int arr1[],int arr2[]);
int is_sorted(int arr[]);
void bubble_sort(int arr[]);
int binary_Search(int arr[], int);

int main(){
int array1[COUNT] = {5, 9, 3, 1, 7, 2, 8, 0, 4, 6};
int array2[COUNT] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int array3[COUNT];

printf(“nPrinting array1:nn”);
print_array(array1);

printf(“nPrinting array2:nn”);
print_array(array2);

printf(“The sum of elements in array1: %dn”, sum_array(array1));
printf(“The sum of elements in array2: %dn”, sum_array(array2));

compare_arrays(array1, array2);

if(is_sorted(array1))
printf(“Array1 is sorted.n”);
else
printf(“Array1 is not sorted.n”);
if(is_sorted(array2))
printf(“Array2 is sorted.n”);
else
printf(“Array2 is not sorted.n”);

printf(“After sorting Array1 is: “);
print_array(array1);
printf(“After sorting Array2 is: “);
print_array(array2);

int key, pos;
printf(“Enter the element to search in array2: “);
scanf(“%d”, &key);
if((pos = binary_Search(array2, key)) == -1)
else
printf(“The element is found in array2 at position %d.n”, pos);
return 0;
}
void print_array(int arr[])
{
int i;
for(i=0; i
printf(” %d”, arr[i]);
printf(“n”);
}
int sum_array(int arr[])
{
int sum = 0;
for(int i = 0; i
sum += arr[i];
return sum;
}
void compare_arrays(int arr1[], int arr2[])
{
//loop
int i;
for(i = 0; i
//- check each element i from both arrays
if(arr1[i] !=arr2[i])
{
//function ends, not the same arrays
printf(“Not the same arrays.n”);
break;
}
if(i == COUNT)
printf(“The arrays are the same.n”);

}
int is_sorted(int arr[])
{
int current_biggest=arr[0];
int sorted_flag = TRUE;
for(int i = 1; i
//loop
//- check if the current element is bigger than the previous element
if(arr[i]
return FALSE;
//- FLAG for if the current element is not bigger than the previous
return TRUE;
}
void bubble_sort(int arr[])
{
int i, j;
for(i = 0; i
{
for(j= 0; j
//check if current element arr[j]>arr[j+1]
// if arr[j] is >, then swap them
int temp= arr[j];
arr[j] = arr [j+1];
arr[j+1] = temp;

}
}
}
int binary_Search(int arr[], int key)
{
int low = 0, high = COUNT-1, mid;
while(low
{
mid = (low + high) / 2.0;
if(key == arr[mid])
return mid;
else if(key
high = mid – 1;
else
low = mid + 1;
}
return -1;
}