Question

Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and...

Pointer arithmetic:

a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing.
b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop. Remember to initialize min and max to “good” values.

The function prototypes and a sample main are provided below:

void printString(char str[]);
void computeMinMax(double arr[],int length,double * min,double * max);

void main(){
char s[ ] = "I like homework";
printString(s);
printf("\n");
double arr[] = {1,-1.1,40,50,3,3,2,3};
double min,max;
computeMinMax(arr,8,&min,&max);
printf("min: %lf max: %lf\n",min,max);
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>

void printString(char str[]);
void computeMinMax(double arr[],int length,double * min,double * max);

void main(){
    char s[ ] = "I like homework";
    printString(s);
    printf("\n");
    double arr[] = {1,-1.1,40,50,3,3,2,3};
    double min,max;
    computeMinMax(arr,8,&min,&max);
    printf("min: %lf max: %lf\n",min,max);
}

void printString(char str[]){
    int i = 0;
    char *p = str;
    while(*(p)!='\0'){
        printf("%c",*p);
        p=p+1;
    }
}

void computeMinMax(double arr[],int length,double * min,double * max){
    int minValue = arr[0], maxValue = arr[0];
    for(int i = 1;i<length;i++){
        if(maxValue < arr[i]){
            maxValue = arr[i];
        }
        if(minValue > arr[i]){
            minValue = arr[i];
        }
    }
    *min = minValue;
    *max = maxValue;
}

Note: Please comment below if you have any doubts. upuote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics...

    2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop....

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • If void * is a pointer to void is a "generic" pointer type, and a void...

    If void * is a pointer to void is a "generic" pointer type, and a void * can be converted to any other pointer type * without an explicit cast, why in the ,myarrcopy function the malloc is created like char and not like void? if we are going to work with different type of arrays? Here is de program: *This is a function that make a copy of any given array. * We then use this function make a...

  • Write a program that replace repeated three characters in a string by the character followed by 3...

    Write a program that replace repeated three characters in a string by the character followed by 3. For example, the string aabccccaaabbbbcc would become aabc3ca3b3cc. When there are more than three repeated characters, the first three characters will be replaced by the character followed by 3. You can assume the string has only lowercase letters (a-z). Your program should include the following function: void replace(char *str, char *replaced); Your program should include the following function: void replace(char *str, char *replaced);...

  • please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function...

    please use c ++ language and write the comment too. thanks Pointer Arithmetic Write a function that passes an array address to a function as a pointer. Using pointer arithmetic, the function determines the average, high, and low value of the array. Your function should have this header: void pointerArithmetic(int *array, int size, double &average, int &high, int &low) Parameter size is the number of elements in array. You may not use the subscript notation [] inside your function –...

  • ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting...

    ***Please complete the code in C*** Write a program testArray.c to initialize an array by getting user's input. Then it prints out the minimum, maximum and average of this array. The framework of testArrav.c has been given like below Sample output: Enter 6 numbers: 11 12 4 90 1-1 Min:-1 Max:90 Average:19.50 #include<stdio.h> // Write the declaration of function processArray int main) I int arrI6]i int min-0,max-0 double avg=0; * Write the statements to get user's input and initialize the...

  • How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include...

    How can I integrate these programs into this menu template: #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include void program_one (); void program_two (); void program_three (); void program_four(); void program_five(); int main() { int menu_option = 0; while (menu_option != 9) { printf(" = 1\n"); //Change this to your first program name. Nothing else. printf(" = 2\n"); //Change this to your second program name. Nothing else. printf(" = 3\n"); //Change this to your third program name. Nothing else. printf(" =...

  • In this assignment, you must complete the 5 functons below. Each function's body must consist of...

    In this assignment, you must complete the 5 functons below. Each function's body must consist of just a single C statement. Furthermore, the statement must not be a control statement, such as if, switch, or any kind of loop. For each problem, you are also restricted to the operators and constants as specified. */ /* 1. Write a function which zeros a variable x. A pointer to x is passed as a parameter. YOU MAY USE NO CONSTANTS in this...

  • Write a function that can return the length of a string in C language. Please leave...

    Write a function that can return the length of a string in C language. Please leave comments to explain the code and verify that it runs properly... Example: Input String: China The length of the string is 5. #include <stdio.h> int length(char * p) {     /write your code here } main(){     int len;     char str[20];     printf("Input string:");     scanf("%s", str);     len = length(str);     printf("The length of string is %d. ", len);     getchar();    ...

  • In C programming Write the implementation for the three functions described below. The functions are called...

    In C programming Write the implementation for the three functions described below. The functions are called from the provided main function. You may need to define additional “helper” functions to solve the problem efficiently. Write a print_string function that prints the characters in a string to screen on- by-one. Write a is_identical function that compares if two strings are identical. The functions is required to be case insensitive. Return 0 if the two strings are not identical and 1 if...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT