Question

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 three will need to be declared as pointers of the appropriate type.

Note: Remember that the address operator & and indirection operator * are complements of each other, and cancel each other out. This will be an important distinction in determining how to write the scanf statements for age, height, and weight.

The second function will accept the five values determined by the previous function, and display the results to the screen in a formatted style, perhaps similar to what you see in the sample output below. Because we are not changing any values in this second function, you will not need to use pointer parameters here.

Your main will be relatively short this time. You will have the variable declarations for storing your five pieces of information, a call to your first function to get the values, and a call to your second function to display them. (To clear the screen in Windows, use a system(“cls”); statement between the two function calls. Clearing the screen is not required for full credit on the lab).

*SAMPLE PROGRAM*

#include 
#include 
#include 

///////// SYMBOLIC CONSTANTS /////////

#define SIZE 1000                       // There is an upper limit on this SIZE, based on the max amount of memory that
                                                        // the program can allocate. If you try to increase it, the program may crash.

#define RANGE 25000                     // A symbolic constant used to determine the range of random numbers generated.




//////// FUNCTION PROTOTYPES /////////

void findMinMax(int valueArr[], int size, int *min, int *max, long *sum, double *avg);
        // Function will determine the min and max values in the array, as well as the average and sum.
        // It returns these values by using the pointer parameters to change the value of the argument passed from main.

void displayStats(int min, int max, long sum, double avg);
        // Function will print out the resulting information in a nicely formatted form.
        // As we are not changing the values here, we do not pass these values by reference this time.

//////// FUNCTION DEFINITIONS ////////

int main()
{
        int randoms[SIZE];              // Array of which will hold SIZE randomly generated ints. This will be our data set                                             
        int index;                              // A loop control variable for our for loops.
        int min, max;                   // These variables (plus sum and average below) will be passed into the findMinMax function by reference.
        long sum;
        double average;                 // Passing them by reference will allow us to change their values here in the main.

        // First, we seed the random number generator. We only want to do this once, so we leave it outside of the loop.
        srand((unsigned)time(NULL));

        // Next, we use a for loop to initialize all SIZE elemements of our array.
        for (index = 0; index < SIZE; index++)
        {
                randoms[index] = rand() % RANGE + 1;
        }

        findMinMax(randoms, SIZE, &min, &max, &sum, &average);  // We call findMinMax to calculate the min, max, sum, and average.
                                                                                                                        // The & address operator before the final four arguments ensures
                                                                                                                        // that they are passed by reference.

        displayStats(min, max, sum, average);

        printf("Press Enter to continue...");
        getchar();
        return 0;
}



void findMinMax(int valueArr[], int size, int *min, int *max, long *sum, double *avg)
{
        int i;  // Our usual loop control variable, which will serve as the array subscript index.

        // Let's initialize any variables we need to initialize.
        // We dereference the pointers using * so we can changes the values they point to, not the address.
        *sum = 0;                       // Sum is an accumulator, so we must start counting from 0.
        *min = valueArr[0];     // To find the min, we will start by assuming the first number is the smallest.
        *max = valueArr[0]; // Like with min, we will assume that the first number is the largest for max.


        for(i = 0; i < size; i++)
        {
                // First, lets add the value in the array at each position to the sum;
                *sum += valueArr[i];

                // Next, we'll use an if statement to update the min, if needed.
                if(valueArr[i] < *min)               
                        *min = valueArr[i];

                // Now we'll do the same thing for max.
                if(valueArr[i] > *max)
                        *max = valueArr[i];
        }

        // Now that we've looped through all of the elements in the array and have the sum total, we can find average.
        // Because we are working with integers, we will need to cast sum to a double so we can get a floating point result.
        *avg = (double)*sum / size;

        // Our function ends at this point. We've calculated all of our values, and have nothing to return because of the void type.
}


void displayStats(int min, int max, long sum, double avg)
{
        printf("Array Summary Statistics:\n");
        printf("-------------------------\n");
        printf("|                       |\n");
        printf("| Minimum Value: %-6d |\n", min);
        printf("| Maximum Value: %-6d |\n", max);
        printf("| Sum: %-17d|\n", sum); 
        printf("| Average: %-13.2f|\n", avg);
        printf("-------------------------\n");
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the c program using pass by reference to read the values of firstname, lastname,age, height, weight.

Program:

#include <stdio.h>

void read(char *namefirst,char *namelast, int *age, double *height, double *weight)
{
printf("Enter the first name: ");
gets(namefirst);
  
printf("Enter the last name: ");
gets(namelast);
  
printf("Enter the age: ");
scanf("%d", age);
  
printf("Enter the height: ");
scanf("%lf", height);
  
printf("Enter the weight: ");
scanf("%lf", weight);
  
printf("\n\n");
}

void printdetails(char *namefirst,char *namelast, int age, double height, double weight)
{
printf(" Details of a person:\n");
printf("-------------------------------\n");

printf("|First name: %-17s|\n", namefirst);
  
  
printf("|Last name: %-17s|\n", namelast);
  
  
printf("|Age: %-17d|\n", age);
  
printf("|Height: %-10.2lf inches|\n", height);
  
  
printf("|Weight: %-10.2lf pounds|\n", weight);

printf("-------------------------------\n");
}

int main()
{
char namefirst[50];
char namelast[50];
int age =0;
double height =0, weight =0;
  
read(namefirst, namelast, &age, &height, &weight);
  
printdetails(namefirst, namelast, age, height,weight);

return 0;
}

Output:

Enter the first name: Lebron
Enter the last name: James
Enter the age: 34
Enter the height: 6.8
Enter the weight: 234


Details of a person:
-------------------------------
|First name: Lebron |
|Last name: James |
|Age: 34 |
|Height: 6.80 inches|
|Weight: 234.00 pounds|
-------------------------------


Screen Shot:

nter the fir t name: Lebron nter the la t name : James ter the age: 34 Enter the height: 6.8 nter the weight: 234 Details of

Add a comment
Know the answer?
Add Answer to:
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...
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
  • 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...

  • 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....

  • ***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...

  • C Programming: Write a program that inputs two strings that represent floating-point values, convert the strings...

    C Programming: Write a program that inputs two strings that represent floating-point values, convert the strings to double values. Calculate the sum,difference,and product of these values and print them. int main(){    // character string value array for user    char stringValue[10];       double sum=0.0;// initialize sum to store the results from 2 double values       double difference=0.0; // initialize difference to store the results from 2 double values       double product = 0.0; // intitialzie product...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below...

    C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...

  • Please program in C++ and document the code as you go so I can understand what...

    Please program in C++ and document the code as you go so I can understand what you did for example ///This code does~ Your help is super appreciated. Ill make sure to like and review to however the best answer needs. Overview You will revisit the program that you wrote for Assignment 2 and add functionality that you developed in Assignment 3. Some additional functionality will be added to better the reporting of the students’ scores. There will be 11...

  • Update your first program to dynamically allocate the item ID and GPA arrays. The number of...

    Update your first program to dynamically allocate the item ID and GPA arrays. The number of items will be the first number in the updated “student2.txt” data file. A sample file is shown below: 3 1827356 3.75 9271837 2.93 3829174 3.14 Your program should read the first number in the file, then dynamically allocate the arrays, then read the data from the file and process it as before. You’ll need to define the array pointers in main and pass them...

  • Using C programming

    Using C, create a data file with the first number being an integer. The value of that integer will be the number of further integers which follow it in the file. Write the code to read the first number into the integer variable how_many.Please help me with the file :((This comes from this question:Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int...

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