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)
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");
}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:

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...
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 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 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 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...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. The function should find all numbers in the array that are greater or equal to the average and fill out the second array with these numbers. You need to design the function. I tried this code but the errors returned were: expression must have a constant value #include<iostream> using...
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 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...
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,...
Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...
/*––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––*/ /* */ /* */ /* This program computes average power, average magnitude */ /* and zero crossings from a speech signal. */ #include <stdio.h> #include <math.h> #define MAXIMUM 50 int main(void) { /* Declare variables */ int k=0, npts=30; double speech[MAXIMUM]={0.000000,-0.023438,-0.031250,-0.031250,-0.039063,-0.039063,-0.023438,0.000000,0.023438,0.070313,-0.039063,-0.039063,0.046875,0.101563,0.117188,0.101563,0.070313,0.054688,0.023438,0.000000,-0.031250,-0.039063,-0.070313,-0.070313,-0.070313,-0.070313,-0.062500,-0.046875,-0.039063,-0.031250}; /* Declare the function prototypes */ /* Compute and print statistics. */ printf("\n SPEECH DATA "); print(,,);//complete the statement printf("\n"); printf(" SPEECH STATISTICS : \n"); printf(" average power: %f \n",);//complete the statement printf(" average magnitude: %f...