Explanation of Solution
a)
Function using Pointers:
The following program demonstrates to take an integer array and adds all elements of the array.
Program:
//Select header files
#include <stdio.h>
#include<iostream>
using namespace std;
//Main function
int main()
{
//Integer array declaration
static int array[5] = { 200, 400, 600, 800, 1000 };
//Declaration of variables
int sum;
//Function for addition
int addnum(int *ptr);
// Store values into variable
sum = addnum(array);
//Display result
cout<< "Sum of all array elements ="<<sum<<"\n";
return 0;
}
Create the function “addnum()” which is used to add integers given in an integer array...
Explanation of Solution
b)
This program takes an integer array and removes all odd elements of array and displays only even elements of the array, the complete program code is shown below:
Program:
//Select header files
#include <iostream>
using namespace std;
//Declare the integer array with values
int ordArray[10] = {21,22,23,24,25,26,27,28,29,30};
int ordArray1[10];
In this construct, a pointer is declared which points to the given integer array. The function declaration is provided and values obtained are been pointed by an integer pointer and finally contents of array is been displayed.
//Main function
int main()
{
//Pointer to ordered array is created
int* ordArrayPtr = ordArray;
Declare the function “removeodd()” to remove odd numbers is been provided and then the values returned by the function are been pointed by an integer pointer
// Function to remove odd numbers
int* removeOddd(int* ordArrayPtr);
// Store the value in integer pointer
int* abc = removeOddd(ordArrayPtr);
Execute for loop to display the integer values present in the array which denotes the final result.
/*Display array values through for loop and loop until “j” value is less than 5*/
for ( int j=0; j < 5; j++ )
cout << *(abc + j) << endl;
//Console window is kept alive after the execution
cin.get();
return 0;
}
This function takes an integer array and removes all odd numbers from the array and displays only even numbers present in the array. It checks for each element in the array by iterating over array using loop and it displays the value only if it is an even number...
Explanation of Solution
Reason:
In the above case, it would be
Trending nowThis is a popular solution!
Chapter 1 Solutions
EBK DATA STRUCTURES AND ALGORITHMS IN C
- Write a function void switchEnds(int *array, int size);that is passed the address of the beginning of an array and the size of the array. The function swaps the values in the first and last entries of the array.arrow_forwardWrite a function that takes an int array as an input and finds the first and second odd numbers. You should find location of these, too. Your function should return all output in function arguments via pointers. Write a main function that reads 10 integers from the user. Store the values in an array and send to the function described above. Take all output from the function and display in main.arrow_forwardWrite a function called double getAverage(int *array, int SIZE), where it accept the array as pointer, and return the average of the array. Print the returned value from this function. Always use *array rather than subscript to access the array value.arrow_forward
- It's important to note that the name of an array by itself is really a pointer to the first element of the array. Therefore passing an array to function is simple since you can just specify the name of the array. Are these statements true?arrow_forwardWrite a function that accepts (but does not read) a 2D array of marks and length (the number of students). The function returns a one-dimensional array with the total marks of the students and the class average. Write the declarations, and show the call to the function from function main. Assume all input and output take place in function main, not in this function, and you do not have to write the code to read in the data. Show the main program and the function call. const int NUM_STUDENTS = 35; const int NUM_ASG = 5; //Purpose: To calculate the total marks for 35 students and the class average //PreCondition: marks is a 2D array already filled with 5 marks for each student // length is the actual number of students with marks //Post Condition: total is a 1D array which is the total of the 5 marks for each student avg is the class average of the total array (for length number of students) void TotalAverage(const double marks[][NUM_ASG], int length,…arrow_forwardWrite a function that accepts (but does not read) a 2D array of marks and length (the number of students). The function returns a one-dimensional array with the total marks of the students and the class average. Write the declarations, and show the call to the function from function main. Assume all input and output take place in function main, not in this function, and you do not have to write the code to read in the data. Show the main program and the function call. const int NUM_STUDENTS = 35; const int NUM_ASG = 5; //Purpose: To calculate the total marks for 35 students and the class average //PreCondition: marks is a 2D array already filled with 5 marks for each student // length is the actual number of students with marks //Post Condition: total is a 1D array which is the total of the 5 marks for each student avg is the class average of the total array (for length number of students) void TotalAverage(const double marks[][NUM_ASG], int length,…arrow_forward
- Write a function ProductMax2() that will take an array as argument andcomputes the product of two maximum numbers in the array. Make sure youdo not use Array subscript notation to iterate over the array.arrow_forwardWrite a function that accepts (but does not read) a 2D array of marks and length (the number of students). The function returns a one-dimensional array with the total marks of the students and the class average. Write the declarations, and show the call to the function from function main. Assume all input and output take place in function main, not in this function, and you do not have to write the code to read in the data. Show the main program and the function call. const int NUM_STUDENTS = 35; const int NUM_ASG = 5; //Purpose: To calculate the total marks for 35 students and the class average //PreCondition: marks is a 2D array already filled with 5 marks for each student // length is the actual number of students with marks //Post Condition: total is a 1D array which is the total of the 5 marks for each student avg is the class average of the total array (for length number of students) void TotalAverage(const double marks[][NUM_ASG], int length, double total[], double& avg); int…arrow_forwardWrite a function that accepts two arguments, an array of integers and a number indicating the number of elements in the array. The function should recursively calculate the sum of all the numbers in the array. Demonstrate the use of the function in a program that asks the user to enter an array of numbers and prints its sum.arrow_forward
- Create a dynamic array that contains an int array. dynamic array, array inside when the existing array is full and a new element needs to be added will double its size, make all the elements of the old array the same will copy the indexes and add the element to the new array. Use the following functions. Write in C language. 1.append(dynamic array, new element): function sent to itself Adds the element sturct to the end of the array inside DynamicArray. Place If not, it behaves in accordance with the dynamic array definition given above. 2. get(index): The element in the index sent to it by the function takes it from the array it hosts and sends it back. ATTENTION ! : Please use a dynamic array and this array with struct structure. I don't want a link list. Do not post wrong or copied codes. Be sure to use the append and get functions given above Please do not solve the question I asked with the same code and copy and paste the code, my right to the question is running out. Solve the…arrow_forwardCreate a dynamic array that contains an int array. dynamic array, array inside when the existing array is full and a new element needs to be added will double its size, make all the elements of the old array the same will copy the indexes and add the element to the new array. Use the following functions. Write in C language. 1.append(dynamic array, new element): function sent to itself Adds the element sturct to the end of the array inside DynamicArray. Place If not, it behaves in accordance with the dynamic array definition given above. 2. get(index): The element in the index sent to it by the function takes it from the array it hosts and sends it back.arrow_forwardusing c++ Define an array of n integer marks and fill it with random numbers, send it to a function that returns an array of failed marks. Hint: use pointer notations onlyarrow_forward
- C++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology PtrC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning