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 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 FillColumnUpward(A, top, bottom, left, z) which takes the array A the top, bottom and left as parameters and iterates through the left column from bottom to top to fill the column.
Define a function to find a given target value in an array, but use pointer notation rather than array notation whenever possible.
Write a function that returns an integer that appears most often with respect to an array of integers   In the array. for example. [1 2 3 2 3 4 2 5] For an array your function must return 2.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education