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
- using 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_forward2. Write a program which sorts the CGPA array having cgpa of all students in ascending order by using bubble sort and make use of pointers for each and every variable and constant and array.arrow_forwardWhat is the difference between void and NULL pointers .Give suitable examples in support of your answer.Give the differences in TABULAR form.arrow_forward
- Write in a programming language (PL), a program to fill an NxN two- dimensional array, in a circular (spiral) pattern, with numbers from 1 to N2 as described below. You can do that by writing first the following functions/procedures which you will call in the main program. The procedure FillRowForward(A, top, left, right, z) which takes the array A the top, left and right as parameters and iterates through top row from left to right to fill the row. z is as used in the algorithm.arrow_forwardWrite a function that receives a two-dimensional array, prints out the data by row along with the sum of the row values. Each value should be printed separated by a space. After the last element in the row has been printed, the sum should be printed on the same line, followed by a newline.arrow_forwardCreate two Arrays A with 10 unsorted integer elements and B with 10 unsorted floatelements. Find the largest element in A and B and then sum both of them calling afunction SumAB() by reference. Sort A and B and then call another function StichAB()to merge B with A. Be careful about memory leakage.arrow_forward
- 5. Using c++, Write a function that determines the index of the second to the last occurrence of a target b in an integer array a. Return -1 if b is not in a or there is only 1 occurrence of b in a.arrow_forwardWhat is the difference between void and NULL pointers .Give suitable examples in support of your answer .arrow_forwardWhat is the difference between void and null pointers.Give suitable example in support of your answerarrow_forward
- Write a program that dynamically allocates an array large enough to hold a user defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in descending order and display the sorted list. Another function should be called that calculates the average score and display in the same function. Use appropriate headings for function and pointer notation rather than array notation whenever possible. Input Validation: Do not accept negative numbers for test scorearrow_forwardQuestion: Given an array of integers, find the maximum and minimum elements present in the array. Your task is to write a function that takes an array of integers as input and returns the maximum and minimum elements as a pair of integers..arrow_forwardWrite a Program for insertion and deletion from an array without replacement of element from the arrays. Also, write a program to search the array for the presence of a given element from an array. Write the sub-functions in the provided test case in text form and upload the same. #include<stdio.h> #include<stdlib.h> int cmpfunc (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int insert_function(int *data, int n, int p, int x) { /* input data: array, n: size of array, p: position of insertion, x: element to insert*/ /*output:-1 indication error or 0 if successful /* write the function for inserting element into a position*/ } int delete_function(int *data, int n, int p, int x) { /* input data: array, n: size of array, p: position of deletion, x: element to deletion*/ /*output:-1 indication error or 0 if successful /* write the function for delete element into a position*/ } int linear_search_function(int *data, int n, int x) { /*write the function to…arrow_forward
- 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