EBK DATA STRUCTURES AND ALGORITHMS IN C
EBK DATA STRUCTURES AND ALGORITHMS IN C
4th Edition
ISBN: 9781285415017
Author: DROZDEK
Publisher: YUZU
Expert Solution & Answer
Book Icon
Chapter 1, Problem 4E

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 Not Easier if the array were unordered

Blurred answer
Students have asked these similar questions
Write a function that accepts an int array and the array’s size as arguments.   The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function should return a pointer to the new array.   Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an array.   The program then passes the array to your array expander function, and displays the values of the new expanded array, one value per line.   You may assume that the file data has at least N values. There are no prompts for the integer and no labels for the expanded reversed array that is printed out. If the integer read in from standard input exceeds 50 or is less than 0 the program terminates silently.
Using only pointers (no array indexing), write a) A function to add all numbers in an integer array. b) A function to remove all odd numbers from an ordered array. The array should remain ordered.
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is two elements larger than the argument array. The first two elements of the new array should be set to 0. Element 0 of the argument array should be copied to element 2 of the array, element 1 of the argument array should be copied to element 3 of the new array, and so forth. The function should return a pointer to the new array. Demonstrate the function in a complete program.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning