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: 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...
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...
C++ visual studios this function has to run twice. and the "count" variable needs to be...
C++ visual studios this function has to run twice. and the "count" variable needs to be "2" on the second run. How can i do that? The parameters can not change as well void printArray(const int *array, int n) {    int count = 1;    char check;    if (check == 'k')    {        count++;    }    cout << "Value " << count << " array contents." << endl;    cout << "------------------------" << endl;   ...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int...
#include <stdio.h> extern unsigned long long sum(unsigned int *array, size_t n); int main(void) { unsigned int array[] = { 1, 1, 1, 3 }; unsigned long long r = sum(array, sizeof(array) / sizeof(*array)); printf("Result is: %llu (%s)\n", r, r == 6 ? "correct" : "incorrect"); return 0; } Complete the code for the line that calls the sum function. Write the sum function.
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
Complete the following C program to solve the parenthesis matching problem using a stack. We studied...
Complete the following C program to solve the parenthesis matching problem using a stack. We studied the problem and the algorithm in class. Given a sequence of chars (symbols), check if each “(”, “{”, or “[” is paired with a matching “)”, “}”, or “[”. For example, correct: ( )(( )){([( )])}     correct: (( )(( ))){([( )])}   incorrect: )(( )){([( )])}     incorrect: ({[ ])}     incorrect: (   #include <stdio.h> #include <stdlib.h> #define size 100 void push(char *s, int* top, char element);...
We see that this computes the product of two matrices. Add a new kernel code, called...
We see that this computes the product of two matrices. Add a new kernel code, called sum, to compute the sum of the two matrices. #include <stdio.h> #include <math.h> #include <sys/time.h> #define TILE_WIDTH 2 #define WIDTH 6 // Kernel function execute by the device (GPU) __global__ void product (float *d_a, float *d_b, float *d_c, const int n) {    int col = blockIdx.x * blockDim.x + threadIdx.x ;    int row = blockIdx.y * blockDim.y + threadIdx.y ;    float...
q : explain the code for a beginner in c what each line do Question 2....
q : explain the code for a beginner in c what each line do Question 2. The following code defines an array size that sums elements of the defined array through the loop. Analyze the following code, and demonstrate the type of error if found? What we can do to make this code function correctly ? #include <stdio.h> #define A 10 int main(int argc, char** argv) { int Total = 0; int numbers[A]; for (int i=0; i < A; i++)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT