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

[Subject: C Programming] 1. Write a function gcd(m,n) that calculates the greate

ID: 3760987 • Letter: #

Question

[Subject: C Programming]

1. Write a function gcd(m,n) that calculates the greatest common divisor of the integers m and n.

2. Write the following function:

float compute_GPA(char grades[], int n);

The grades array will contain letter grades(A,B,C,D, or F, either upper-case or lower-case); n is the length of the array. The function should return the average of the grades(assume that A=4, B=3, C=2, D=1, and F=0).

3. Suppose that the following declarations are in effect:

int a[] = {5, 15, 34, 54, 14, 2, 52, 72};

int *p = &a[1], *q = &a[5];

a. What is the value of *(p+3)?

b. What is the value of *(q-3)?

c. What is the value of q-p?

d. Is the condition p true or false?

e. Is the condition *p<*q true or false?

4.What will the contents of the a array after the following statements are executed?

# define N 10

int a[N] = {1,2,3,4,5,6,7,8,9,10};

int *p = &a[0], *q = &a[N-1], temp;

while (p

temp = *p;

*p++ = *q;

*q-- = temp; }

5. Rewrite the following function to use pointer arithmetic instead of array subscripting(In other words, eliminate the variable i and all uses of the [] operator.) Make as few changes as possible.

void store_zeros(int a[], int n){

int i;

for(i=0, i

a[i] = 0; }

6. Write the following function:

double inner_product(const double *a, const double *b, int n);

a and b both point to arrays of length n. The function should return a[0] * b[0] + a[1] * b[1] + … + a[n-1] * b[n-1]. Use pointer arithmetic not subscripting to visit array elements.

Explanation / Answer

method gcd(m,n):

//calculates greatest common divisor of two integers m and n

     int gcd(int n,int m)

     {

          //if m is less than n and m exactly divides n, return m as gcd of m and n

          if(m<=n && n%m == 0)

              return m;

          //if n is less than m, call gcd again by alternating the positions of m and n

          if(n < m)

              return gcd(m,n);

          //Euclids formula to calculate gcd of m and n

          else

              return gcd(m,n%m);

     }

Sample output:

2)

//calculates and returns average of GPA

     float compute_GPA(char grades[],int n)

     {

          float avg_gpa=0,total_gpa=0;

          int i;

          for(i=0;i<n;i++)

          {

              if(grades[i]=='A'||grades[i]=='a')

                   total_gpa=total_gpa+4;

              else if(grades[i]=='B'||grades[i]=='b')

                   total_gpa=total_gpa+3;

              else if(grades[i]=='C'||grades[i]=='c')

                   total_gpa=total_gpa+2;

              else if(grades[i]=='D'||grades[i]=='d')

                   total_gpa=total_gpa+1;

              else if(grades[i]=='F'||grades[i]=='f')

                   total_gpa=total_gpa+0;

          }

          avg_gpa=(float)total_gpa/n;

              return avg_gpa;

     }

3)

#include <stdio.h>

     int main()

     {

          int a[] = {5, 15, 34, 54, 14, 2, 52, 72};

          int *p = &a[1], *q = &a[5];

          printf("the value of *(p+3) is %d ",*(p+3));

          printf("the value of *(q-3) is %d ",*(q-3));

          printf("the value of (q-p) is %ld ",(q-p));

          if(p)

              printf("p is true ");

          else

              printf("p is false");

          if(*p<*q)

              printf ("condition *p<*q is true ");

          else

              printf("condition *p<*q is false ");

          return 0;

     }

Sample output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote