Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. (30 pts) Develop a structured C program to calculate the perimeter of a trian

ID: 3728376 • Letter: 2

Question

2. (30 pts) Develop a structured C program to calculate the perimeter of a triangle. Your program must be structured as follows: main program and one function Main program inputs all three sides a, b and c, and calls the function P by using a, b and c as input. Print the returned value from function P on screen. Function P takes data from the main program. If a+bcc or ateeb or bteca, print an error message "This is not a triangle!" and return -1 to the main. Else, return a+b+c. (1) Run the code for a-3, b-4 and c-8, and show the screenshot of the result (2) Run the code for a=5, b-6 and c= 10, and show the screenshot of the result 1l-by-reference (that is, use of pointers), to pass the array to a function, named Summation. In main: define array, pass array, print out the array and the result returned from Summation on screen In function Summation: 1) take array and size from main 2) sum each element according to the following rules 3) if the value of f(i) is positive, add 3*f(i) to the sum 4) if the value of f(i) is negative, add -2* f(i) to the sum 5) return the final value of sum to main NOTE: you must use call-by-reference (the pointer) to call functions, as required otherwise, no credits will be given.

Explanation / Answer

#include <stdio.h>

// function that performs summation on array passed by reference
int summation(int *farray,int arraySize)
{
int sum=0; // local variable to hold sum result
for(int i =0; i < arraySize ; i ++,farray++)
{
if(*farray >= 0) // number is positive
sum += 3*(*farray);
else // number is negative
sum += (-2)*(*farray);
}
return sum;
}

int main(void)
{
int f[]={11,-23,34,-8,-10,18,29};

// variable to hold the size of array
int size = sizeof(f)/sizeof(f[0]);

// function summation is called with parameters f- array passed by reference and its size
int sum = summation(f,size);

printf(" Original array: ");
for(int i =0;i < size;i ++)
printf("%d ",f[i]);

printf(" Summation result: %d",sum);
return 0;

}

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote