Question

In: Computer Science

goal of this function will be to add an element to an already existing array in...

goal of this function will be to add an element to an already existing array in C/C++.

bool add(structure dir[], int& size, const char nm[], const char ph[]){

//code here

}

add = function name

structure = structure with two fields

{

char name[20];

char phone[13];

}

int& size = # of entries in dir

nm = name that's going to be added

ph = phone that's going to be added.

return true if entry was successfully added, false if there's no more room.

if entry was added, size will reflect the new number of entries in dir.

Solutions

Expert Solution

Please look at my code and in case of indentation issues check the screenshots.

---------------main.cpp---------------

#include <iostream>
#include <string.h>
using namespace std;
#define MAX 1000


struct phoneBook       //structure with two fields
{
   char name[20];
   char phone[13];
};

//to add an element to an already existing array
bool add(struct phoneBook dir[], int& size, const char nm[], const char ph[]){
   if(size == MAX)                   //checks if number of elements is equal to the max capacity
       return false;               //returns false, since new element cannot be added NO ROOM
   strcpy(dir[size].name, nm);       //copy nm to array location at index size
   strcpy(dir[size].phone, ph);   //copy ph to array location at index size
   size++;                           //increase size
   return true;                   //return true since element is added successfully at the end
}


int main()
{
   struct phoneBook dir[MAX];       //array of structure type, that can store 1000 name,phone numbers  
   int size = 0;                   //# of entries currently in dir
   int val;

   val = add(dir, size, "Watson", "9999999999");       //add first element
   if(val){
       cout << "Size is " << size << endl;
       cout << "Entry added successfully" << endl;
    }

   val = add(dir, size, "John", "8888888888");           //add second element
   if(val){
       cout << "Size is " << size << endl;
       cout << "Entry added successfully" << endl;
    }

   val = add(dir, size, "Jim", "7777777777");           //add third element
   if(val){
       cout << "Size is " << size << endl;
       cout << "Entry added successfully" << endl;
    }

    cout << "\nThe phoneBook has the following entries:\n";   //print all elements
    for(int i = 0; i < size; i++){
        cout << dir[i].name << " " << dir[i].phone << endl;
    }

   return 0;
}

--------------Screenshots-------------------

------------------Output-----------------

------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs down.
Please Do comment if you need any clarification.
I will surely help you.

Thankyou


Related Solutions

Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new...
Write a Java Program that can:​ Remove a particular element from an array.​ Add a new element to an array.​ Change an element with the new one.​ Search for a particular element in the array.​ ​The code must have four separate methods for each task stated above.​ Do not use any pre-defined Java functions.​ You are free to use int or String data-type for the array.​
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array...
THE FOLLOWING IS CODED IN C Write a function that sets each element in an array to the sum of the corresponding elements in two other arrays. That is, if array 1 has the values 2,4, 5, and 8 and array 2 has the values 1, 0, 4, and 6, the function assigns array 3 the values 3, 4, 9, and 14. The function should take three array names and an array size as arguments. Test the function in a...
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array.
C++ ProgramWrite a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function...
Element Shifter: Write a function that accepts an int array and the array’s size as arguments....
Element Shifter: Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The...
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size...
C++ 9.12: Element Shifter Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so...
There is already an existing class called Sizes, with an existing main method. Inside the Sizes...
There is already an existing class called Sizes, with an existing main method. Inside the Sizes class, create a method called menu. The purpose of this method is to display the menu on the screen and allow the user to make a choice from the menu. Do not change any of the existing code in the main method, and do not add code to the main method. Changing the main method in any way will give you an automatic grade...
Write a loop that subtracts 1 from each element in lowerScores. If the element was already...
Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the element. Ex: lowerScores = {5, 0, 2, -3} becomes {4, 0, 1, 0}. please bold the solution import java.util.Scanner; public class StudentScores { public static void main (String [] args) { Scanner scnr = new Scanner(System.in); final int SCORES_SIZE = 4; int[] lowerScores = new int[SCORES_SIZE]; int i; for (i = 0; i < lowerScores.length; ++i) {...
Add Instance to the Array List Instances in an Array Search for an Instance in the...
Add Instance to the Array List Instances in an Array Search for an Instance in the Array Delete an Instance from the Array Update an Instance in the Array Save Array Elements to CSV File Exit Program The requirement of this assignment is to design and implement a system in Java that will manage instantiated instances of the Objects created in the User Designed Object Assignment. This is the Young Adult Object. The system should be able to add an...
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT