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.​
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...
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) {...
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...
Given an array, return the element at which the "pattern" breaks. For example, in the array...
Given an array, return the element at which the "pattern" breaks. For example, in the array [2,4,6,8,11,13,15,17], the algorithm should return 11 because the difference between every previous node was 2, and the difference between 8 and 11 is 3. PLEASE USE DIVIDE AND CONQUER. Thank you. Python please or just a description of the algorithm
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
The goal is to Create an array of struct “employee” Fill the array with information read...
The goal is to Create an array of struct “employee” Fill the array with information read from standard input using C++ style I/O Shuffle the array Select 5 employees from the shuffled array Sort the shuffled array of employees by the alphabetical order of their last Name Print this array using C++ style I/O Random Number Seeding We will make use of the random_shuffle() function from the standard algorithm library. You can use srand() function provided in with a seed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT