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
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education