Question

Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...

Please answer the following C question:

Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c

The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct.

The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models.

Please explain the logic errors present in in the definition of is_reverse_sorted and all_different functions and include the final code for the file array-utils5A.c

File array-utils5A.c

// ATTENTION: The definitions given below for is_reverse_sorted
// and all_different are DEFECTIVE!

#include <assert.h>

#include "array-utils5A.h"

int is_reverse_sorted(const int *a, int n)
{
assert (n >= 1);

int i;
for (i = 0; i < n - 1; i++) {
if (a[i - 1] < a[i])
return 0;
else
return 1;
}
return 1;
}

int all_different(const int *a, int n)
{
assert(n > 1);

int i, j, result;
for (i = 0; i < n; i++) {
for (j = 0; j < i; j++) {
if (a[i] == a[j])
result = 0;
else
result = 1;
}
}

return result;
}
  
#ifdef UNIT_TESTS
#include <stdio.h>

// This macro works for variables declared to be arrays. (DON'T try to
// use it for function parameters declared to be arrays!)
#define COUNT(x) (sizeof(x) / sizeof(x[0]))

void test_is_reverse_sorted(const char *tag,
const int *a, int n, int expected_rv);

void test_all_different(const char *tag,
const int *a, int n, int expected_rv);

int main(void)
{
int test_01[] = {10, 10, 10, 10, 10};
int test_02[] = {11};
int test_03[] = {15, 14, 13, 12, 11};
int test_04[] = {20, 19, 18, 19, 17, 16};
int test_05[] = {25, 24, 25, 21, 21, 20};
test_is_reverse_sorted("test_01", test_01, COUNT(test_01), 1);
test_is_reverse_sorted("test_02", test_02, COUNT(test_02), 1);
test_is_reverse_sorted("test_03", test_03, COUNT(test_03), 1);
test_is_reverse_sorted("test_04", test_04, COUNT(test_04), 0);
test_is_reverse_sorted("test_05", test_05, COUNT(test_05), 0);
fputc('\n', stdout);

int test_06[] = { 30, 31 };
int test_07[] = { 32, 35, 34, 36, 31 };
int test_08[] = { 40, 41, 42, 43, 44, 40 };
int test_09[] = { 50, 50, 51, 52, 53};
int test_10[] = { 60, 61, 62, 63, 64, 64 };
test_all_different("test_06", test_06, COUNT(test_06), 1);
test_all_different("test_07", test_07, COUNT(test_07), 1);
test_all_different("test_08", test_08, COUNT(test_08), 0);
test_all_different("test_09", test_09, COUNT(test_09), 0);
test_all_different("test_10", test_10, COUNT(test_10), 0);
fputc('\n', stdout);

return 0;
}

void test_is_reverse_sorted(const char *tag,
const int *a, int n, int expected_rv)
{
printf("is_reverse_sorted test case with tag \"%s\":", tag);
int actual_rv = is_reverse_sorted(a, n);
if (actual_rv == expected_rv)
printf(" Pass.\n");
else
printf(" FAIL: expected %d but got %d.\n", expected_rv, actual_rv);
}

void test_all_different(const char *tag,
const int *a, int n, int expected_rv)
{
printf("all_different test case with tag \"%s\":", tag);
int actual_rv = all_different(a, n);
if (actual_rv == expected_rv)
printf(" Pass.\n");
else
printf(" FAIL: expected %d but got %d.\n", expected_rv, actual_rv);
}

#endif // #ifdef UNIT_TESTS

File array-utils5A.h

int is_reverse_sorted(const int *a, int n);
// REQUIRES: n >= 1. Elements a[0] ... a[n-1] exist.
// PROMISES
// Return value is 1 if n == 1, or if n > 1 and all of
// a[0] >= a[1] ... a[n-2] >= a[n-1] are true.
// Otherwise, return value is 0.

int all_different(const int *a, int n);
// REQUIRES: n > 1. Elements a[0] ... a[n-1] exist.
// PROMISES: Return value is 1 if no two elements among
// a[0] ... a[n-1] are equal to each other.
// Otherwise, return value is 0.

int is_alternating(const int *a, int n);
// Decides whether array elements form an alternating sequence. In an
// alternating sequence, each number has the opposite sign of the
// previous number in the sequence. 0 is neither positive nor negative,
// so for the purposes of this exercise, a sequence with 0 it is not
// alternating.
//
// Examples: {-1, 1, -1, 1} and {4, -1, 2, -3, 5} are alternating
// sequences, but {-2, 3, 4, -5} and {1, -2, 0, -3} are not.
//
// REQUIRES: n >= 1. Elements a[0] ... a[n-1] exist.
// PROMISES
// The return value is 1 if n == 1.
// If n > 1, the return value is 1 if a[0] ... a[n-1] form
// an alternating sequence.
// PROMISES
// Otherwise, the return value is 0.

Homework Answers

Answer #1

// array_utils5A.h
#ifndef ARRAY_UTILS_H
#define ARRAY_UTILS_H

int is_reverse_sorted(const int *a, int n);
// REQUIRES: n >= 1. Elements a[0] ... a[n-1] exist.
// PROMISES
// Return value is 1 if n == 1, or if n > 1 and all of
// a[0] >= a[1] ... a[n-2] >= a[n-1] are true.
// Otherwise, return value is 0.

int all_different(const int *a, int n);
// REQUIRES: n > 1. Elements a[0] ... a[n-1] exist.
// PROMISES: Return value is 1 if no two elements among
// a[0] ... a[n-1] are equal to each other.
// Otherwise, return value is 0.

int is_alternating(const int *a, int n);
// Decides whether array elements form an alternating sequence. In an
// alternating sequence, each number has the opposite sign of the
// previous number in the sequence. 0 is neither positive nor negative,
// so for the purposes of this exercise, a sequence with 0 it is not
// alternating.
//
// Examples: {-1, 1, -1, 1} and {4, -1, 2, -3, 5} are alternating
// sequences, but {-2, 3, 4, -5} and {1, -2, 0, -3} are not.
//
// REQUIRES: n >= 1. Elements a[0] ... a[n-1] exist.
// PROMISES
// The return value is 1 if n == 1.
// If n > 1, the return value is 1 if a[0] ... a[n-1] form
// an alternating sequence.
// PROMISES
// Otherwise, the return value is 0.

#endif // ARRAY-UTILS_H

//end of array_utils5A.h

// array-utils5A.c
// ATTENTION: The definitions given below for is_reverse_sorted
// and all_different are DEFECTIVE!

#include <assert.h>
#include "array-utils5A.h"

int is_reverse_sorted(const int *a, int n)
{
assert (n >= 1);

int i;
// loop from first to second last element of array a
for (i = 0; i < n - 1; i++) {
if (a[i] < a[i+1]) // ith element < (i+1)th element, a not reverse sorted
return 0;
}

return 1;
}

int all_different(const int *a, int n)
{
assert(n > 1);

int i, j, result = 1; // set result to 1
// loop over the array
for (i = 0; i < n; i++) {
// loop from start to i-1
for (j = 0; j < i; j++) {
if (a[i] == a[j]) // elements at indices i and j are equal, then all elements are not different
result = 0;
}
}

return result;
}

int is_alternating(const int *a, int n)
{
assert(n > 1);
int i;
// loop over the array from first to second last element
for( i=0;i<n-1;i++)
{
if(a[i] == 0) // ith element is 0
return 0;
else if((a[i] > 0 && a[i+1] > 0) || (a[i] < 0 && a[i+1] < 0)) // i and i+1 elements are both of same sign, then not alternating
return 0;
}

return 1;
}

#include <stdio.h>

// This macro works for variables declared to be arrays. (DON'T try to
// use it for function parameters declared to be arrays!)
#define COUNT(x) (sizeof(x) / sizeof(x[0]))

void test_is_reverse_sorted(const char *tag,
const int *a, int n, int expected_rv);

void test_all_different(const char *tag,
const int *a, int n, int expected_rv);

void test_is_alternating(const char *tag,
const int *a, int n, int expected_rv);


int main(void)
{
int test_01[] = {10, 10, 10, 10, 10};
int test_02[] = {11};
int test_03[] = {15, 14, 13, 12, 11};
int test_04[] = {20, 19, 18, 19, 17, 16};
int test_05[] = {25, 24, 25, 21, 21, 20};
test_is_reverse_sorted("test_01", test_01, COUNT(test_01), 1);
test_is_reverse_sorted("test_02", test_02, COUNT(test_02), 1);
test_is_reverse_sorted("test_03", test_03, COUNT(test_03), 1);
test_is_reverse_sorted("test_04", test_04, COUNT(test_04), 0);
test_is_reverse_sorted("test_05", test_05, COUNT(test_05), 0);
fputc('\n', stdout);

int test_06[] = { 30, 31 };
int test_07[] = { 32, 35, 34, 36, 31 };
int test_08[] = { 40, 41, 42, 43, 44, 40 };
int test_09[] = { 50, 50, 51, 52, 53};
int test_10[] = { 60, 61, 62, 63, 64, 64 };
test_all_different("test_06", test_06, COUNT(test_06), 1);
test_all_different("test_07", test_07, COUNT(test_07), 1);
test_all_different("test_08", test_08, COUNT(test_08), 0);
test_all_different("test_09", test_09, COUNT(test_09), 0);
test_all_different("test_10", test_10, COUNT(test_10), 0);
fputc('\n', stdout);

int test_11[] = {-1, 1, -1, 1};
int test_12[] = {4, -1, 2, -3, 5};
int test_13[] = {-2, 3, 4, -5};
int test_14[] = {1, -2, 0, -3};
test_is_alternating("test_11", test_11, COUNT(test_11), 1);
test_is_alternating("test_12", test_12, COUNT(test_12), 1);
test_is_alternating("test_13", test_13, COUNT(test_13), 0);
test_is_alternating("test_14", test_14, COUNT(test_14), 0);

fputc('\n', stdout);


return 0;
}

void test_is_reverse_sorted(const char *tag,
const int *a, int n, int expected_rv)
{
printf("is_reverse_sorted test case with tag \"%s\":", tag);
int actual_rv = is_reverse_sorted(a, n);
if (actual_rv == expected_rv)
printf(" Pass.\n");
else
printf(" FAIL: expected %d but got %d.\n", expected_rv, actual_rv);
}

void test_all_different(const char *tag,
const int *a, int n, int expected_rv)
{
printf("all_different test case with tag \"%s\":", tag);
int actual_rv = all_different(a, n);
if (actual_rv == expected_rv)
printf(" Pass.\n");
else
printf(" FAIL: expected %d but got %d.\n", expected_rv, actual_rv);
}

void test_is_alternating(const char *tag,
const int *a, int n, int expected_rv)
{
printf("is_alternating test case with tag \"%s\":", tag);
int actual_rv = is_alternating(a, n);
if (actual_rv == expected_rv)
printf(" Pass.\n");
else
printf(" FAIL: expected %d but got %d.\n", expected_rv, actual_rv);
}

// end of array-utils5A.c

Output:

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 the C programming language implement Heapsort in the manner described in class. Here is some...
Using the C programming language implement Heapsort in the manner described in class. Here is some example code to use as a guideline. Remember, you need only implement the sort algorithm, both the comparison and main functions have been provided. /* * * after splitting this file into the five source files: * * srt.h, main.c, srtbubb.c, srtinsr.c, srtmerg.c * * compile using the command: * * gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR |...
Please answer the following C question: Read the files vec5C.h, vec5C.c, and main5C.h. Build an executable...
Please answer the following C question: Read the files vec5C.h, vec5C.c, and main5C.h. Build an executable using gcc -Wall main5C.c vec5C.c Do this: Add function definitions to vec5C.c so that functions are available for dot product, sum, and cross product. Do not change the function prototypes in vec5C.h. Then add code to main to call these functions and display the dot product of u and v, the sum of u and v, and the cross product of u and v....
Please answer the following C question: There is a documented prototype for a function called get_a_line...
Please answer the following C question: There is a documented prototype for a function called get_a_line in the code below. Write a definition for get_a_line—the only function called from that definition should be fgetc. #include <stdio.h> #include <string.h> #define BUFFER_ARRAY_SIZE 10 int get_a_line(char *s, int size, FILE *stream); // Does what fgets does, using repeated calls to fgetc, but // provides a more useful return value than fgets does. // // REQUIRES // size > 1. // s points to...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code...
Write a MIPS assembly program that sorts an array using bubble sort translating the C code int main(void) { int array[] = {10, 2, 7, 5, 15, 30, 8, 6}; // input array int arraySize = sizeof(array)/sizeof(array[0]); bool swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; //Note : "j" , "arraySize - j" are optimizations to the bubble sort algorithm j++; // j= sorted elements int i=0; /* "arraySize - j" is used...
I am running this code using C to count the words in char array, but somehow...
I am running this code using C to count the words in char array, but somehow it keeps counting the total characters + 1. Any solution to fix this problem? #include <stdio.h> int main() {    char input[1001];    int count =0;    fgets(input, 1001, stdin);    char *array = input;    while(*array != '\0')       {        if(*array == ' '){            array++;        }        else{        count++;        array++;...
Consider the following variable definitions: char a, *b, *c; int d[2], *e; int i, *j; How...
Consider the following variable definitions: char a, *b, *c; int d[2], *e; int i, *j; How many total bytes does this code allocate for variables?  Assume a 32-bit representation for integer and pointer values. a     sizeof(char) b     sizeof(char *) c     sizeof(char *) d    2*sizeof(int) e     sizeof(int *) i     sizeof(int) j     sizeof(int *) Total number of bytes ? What is the output of the following piece of code? (Use the above variable definitions). j = &i; *j = 50;                      /* i = 50 */ e = &d[0]; *e...
Hi there, I've been asked to write a program in C which can read values from...
Hi there, I've been asked to write a program in C which can read values from a file then sort them, and then write to a binary file. I'm getting stuck when I write my binary file as the output is just spitting out garbage values and not the values that are being read in. When I print my input file reader everything is perfect but after sorting and then writing, the output is completely wrong. I have checked that...
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not need to check values returned by malloc nor calloc). typedef struct tv_show { char *name; int num_channels, *channels; } Tv_show; a. Implement the init_tv_show function that...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is always zero? Please fix and explain! #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int BinarySearch(const int A[],const int L,const int R,const int key) {              if (R >= 1){           int middle = L + (R-1)/2;                              if (A[middle] == key)        return A[middle];               if (A[middle] > key){...