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.
// 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:
Get Answers For Free
Most questions answered within 1 hours.