Using the C language write a program as the one follows The functions you will w
ID: 675091 • Letter: U
Question
Using the C language write a program as the one follows
The functions you will write will all be in a single file named Arrays.c.
Include the header files:
stdio.h, stdlib,h, string.h.
Include the line:
typedef char *String;
that will create the type alias String.
Define the symbolic integer constant N which will contain the number of elements in the arrays that we are dealing with. It’s value should be set to 10.
Define the function:
double arraySum(double a[], int n)
that will sum up the first n elements in the passed array using a for loop to index through the elements of the array.
Define another function:
double arraySumPtr(double a[], int n)
that will also sum up the elements of the array but this time use a pointer rather than indexing through the elements.
Define the function:
void arrayCopy(double src[], double dst[], int n)
that will copy the first n elements from src to dst. This should be done using pointers rather than array indexing.
Define the function:
String toString(double a[], int n)
that will create a string with the value:
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
if called on the arr array. Note: being a general-purpose function it must work on an empty array, an array with only one element, and arrays of more than one element. Between the element values there must be a comma followed by a space. The whole array is surrounded by square brackets. Use the function sprintf() to convert numbers to strings (read K&R or look it up online for reference).
Define the main() function which will exercise the above functions:
Section 1.
i.
Section 1.
i.Declare the array arr containing N elements of type double.
ii.Initialize the elements such that arr[i] = (double)i + 1.0.
iii.Compute the value sumA by calling arraySum().
iv.Compute the value sumP by calling arraySumPtr().
v.Display the values of the two variables you just computed.
vi.Display an extra blank line between sections.
.Section 2.
.Declare the array arrCopy containing N elements of type double.
.Call the function arrayCopy() to copy the elements of arr into the array arrCopy.
.Using the function toString(), convert the two arrays each into a string and display the contents of each array preceded by the name of the array, a colon (:), and spaces (to line up the two arrays).
.Since the String output from toString() is an allocated buffer, remember to free() the allocated storage.
.Display an extra blank line between sections.
.Section 3.
.Declare the pointer to a double, arrDup, and allocate space for an array containing N elements of type double.
.Call the function arrayCopy() to copy the elements of arr into the array arrDup.
.Using the function toString(), convert these two arrays each into a string and display the contents of each array preceded by the name of the array, a colon (:), and spaces (to line up the two arrays).
.Since the String output from toString() is an allocated buffer, remember to free() the allocated storage.
.Display an extra blank line between sections.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef char *String;
double arraySum(double a[], int n) {
double sum = 0;
int i = 0;
for(i = 0; i < n; i++)
sum += a[i];
return sum;
}
double arraySumPtr(double a[], int n) {
double *ptr = &a[0];
double sum = 0;
int count = 0;
while(count < n) {
sum += *ptr;
//printf("%f, %f ", *ptr, sum);
ptr++;
count++;
}
return sum;
}
void arrayCopy(double src[], double dst[], int n) {
int i = 0;
for(i = 0; i < n; i++)
dst[i] = src[i];
}
String toString(double a[], int n) {
String str ;
str[0]= '[';
int i = 0;
int count = 1;
for(i = 0; i < n; i++) {
if(i > 0)
str[count++] = ',';
str[count++] = (char)a[i];
}
str[count++] = ']';
return str;
}
int main() {
int n = 10;
double arr[n];
int i = 0;
for(i = 0; i < n; i++)
arr[i] = (double) i + 1.0;
double sumA = arraySum(arr, n);
double sumB = arraySumPtr(arr, n);
printf("sum A = %f, sum B = %f ", sumA, sumB);
double dest[n];
arrayCopy(arr, dest, n);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.