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
THIS IS NOT A GRADING ASSIGNMENT: Please only do lab 2.2 (bottom part of the first picture) For that Lab 2.2 do: *Part 1 *Part 2 *Part 3 I also attached Section 2.5.2 which is part of the step 1 so you can read what is it about.   Thank you!
can you please give me: * the code (step 3) *the list file (step 5) *and answer step 6   Thank you
# Find the error# Why will the following code not print out a list of contact namesphoneBook = {'Doe, Jane' : '843-000-0000' ,'Doe, John' : '843-111-1111' ,'Smith, Adam' : '843-222-2222' ,'Jobs, Steve' : '999-333-3333' ,}for contact in phoneBook.values():print(contact)
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:9781337102124
Author:Diane Zak
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Cengage Learning