Question

Write one single program that does all of the following tasks, in order. You should practice...

Write one single program that does all of the following tasks, in order. You should practice proper Top Down Design and Procedural Abstraction techniques unless otherwise noted.

  1. -Write a program with the following functions, with the correct number of arguments for each function.

-void fillUpArray ( argument1, argument2) This function should read in a text file called "villagers.txt" (You will create your own). "villagers.txt" is a text file of numbers in ascending order, ranging anywhere from 1 to 100, nonrepeating. Each number is separated by a new line character. The very first number in the text file is a number that refers to how many entries are in the rest of the text file. This function should populate an array with all the remaining entries in the text file, but it should NOT declare any arrays itself and it should not reference any global variables.

-void printArray (argument1, argument2) This function should print out the contents of an array, with a space between each item.

-any other functions used in class.

-You should use the concept of partially filled arrays by declaring an array of some arbitrarily large size, and having a variable "numberUsed" that will eventually store the number of values in the array. Do this in the main function, and then make the appropriate function calls to accomplish the following task:

       -After the array is populated, the program should then output the array and ask the user to input 2 numbers, one at a time, discarding all excess input. Other input validation is NOT needed. The program will then search the array (in an efficient manner) for these 2 numbers, swap their positions in the array, and output the new array. If either one of the numbers isn't in the array, the program should output an appropriate message instead.

The swapping of the two values MUST be done with a function call, and MUST be done with the mySwap function presented in class.

Example village.txt (the first number 7 means there are 7 additional numbers afterwards)

7

6

9

10

14

16

29

56

(Possible Correct Output)

The array is: 6 9 10 14 16 29 56

Enter a number to search for: 10

Enter another number to search for: 29

New Array: 6 9 29 14 16 10 56

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

This Solution contains an Explanation to solve the problem, full c++ code, proper comments with code and

finally execution of the program for your better and full understanding.

Explanation:

Before going to the direct coding part, let's understand the approach to solve the problem.

First, create a village.txt file with valid data.

Once this is done then next read the file and parse file numbers into an array.

now take two inputs numbers and search these two numbers in the array.

One can search linearly but this will cost to O(n) time.

so Why not search numbers in lesser time.

since the array is sorted, Hence Use Binary Search to search the numbers in the sorted array.

This will cost to O(log N) time.

If both numbers are found then swap by taking the third variable.

Now let's see the whole code for better visualization.

village.txt:

7
6
9
10
14
16
29
56

C++ code:

searchAndSwap.cpp

#include <iostream>

#include <fstream>

using namespace std;

// binary search to get index

int binSearch(int arr[], int low, int high, int num) {

// check low <= high

if (low <= high) {

// middle index

int middle = (low + high)/2;

// check for num to match

if (arr[middle] == num){

return middle ;

}

// num is in left side

if (arr[middle] > num){

return binSearch(arr, low, middle-1, num);

}

// num is in right side

if (arr[middle] < num){

return binSearch(arr, middle+1, high, num);

}

}

// return for not found

return -1;

}

// swap the numbers in array

void mySwap(int arr[],int index1,int index2){

// temp var

int temp = arr[index1];

// replace index1 num with index2 num

arr[index1] = arr[index2];

// again replace index2 num with index1 num

arr[index2] = temp;

}

int main(int argc, char * argv[]){

// given file name

string fileName = "village.txt";

int n,i=-1;

int temp,num1,num2;

std::fstream fp(fileName, std::ios_base::in);

// read first integer, which is size of array

fp >> n;

// CREATING ARRAY

int arr[n];

while (fp >> temp){

arr[++i] = temp;

printf("\n%d", temp);

}

// print arr

cout<<"\nThe array is: ";

for(int i=0;i<n;i++){

cout<<arr[i]<<" ";

}

cout<<"\nEnter a number to search for: ";

cin>>num1;

cout<<"Enter another number to search for: ";

cin>>num2;

int index1,index2;

// search both number in sorted array

index1 = binSearch(arr,0,n-1,num1); // args: array,startIndex,endIndex,num

index2 = binSearch(arr,0,n-1,num2);

// check for found or not

if(index1 == -1){

cout<<endl<<num1<<" is NOT present in array";

}

if(index2 == -1){

cout<<endl<<num2<<" is NOT present in array";

}

if(index1 != -1 && index2 != -1){

// call func to swap the numbers

mySwap(arr,index1,index2);

}

// print the array

cout<<"\nNew Array: ";

for(int i=0;i<n;i++){

cout<<arr[i]<<" ";

}

// return from main func

return 0;

}

Screenshots:

This code is compiled and executed in terminal ubuntu 18.04.

E Solution: Untitled-2 • Solution: Untitled-1 G searchAndSwap.cpp X main(int,char * []), village.txt home > jay > HomeworkLib > C sG searchAndSwap.cpp X main(int,char * []), village.txt E Solution: Untitled-2 • E Solution: Untitled-1 home > jay > HomeworkLib > 6

Since everything is provided for your complete understanding,

However, if any queries regarding this problem then don't hesitate to ASK. I will surely answer you.

Add a comment
Know the answer?
Add Answer to:
Write one single program that does all of the following tasks, in order. You should practice...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java. Write a GUI contact list application. The program should allow you to input names...

    In Java. Write a GUI contact list application. The program should allow you to input names and phone numbers. You should also be able to input a name and have it display the previously entered phone number. The GUI should look something like the following, although you are welcome to format it in any way that works. This should be a GUI application with a JFrame. The program should contain two arrays of Strings. One array will contain a list...

  • Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a funct...

    Program in C++! Thank you in advance! Write a menu based program implementing the following functions: (1) Write a function that prompts the user for the name of a file to output as a text file that will hold a two dimensional array of the long double data type. Have the user specify the number of rows and the number of columns for the two dimensional array. Have the user enter the values for each row and column element in...

  • Write a menu based program implementing the following functions: (0) Write a function called displayMenu that...

    Write a menu based program implementing the following functions: (0) Write a function called displayMenu that does not take any parameters, but returns an integer representing your user's menu choice. Your program's main function should only comprise of the following: a do/while loop with the displayMenu function call inside the loop body switch/case, or if/else if/ ... for handling the calls of the functions based on the menu choice selected in displayMenu. the do/while loop should always continue as long...

  • Write a C program convert.c that converts each number in an array by the sum of...

    Write a C program convert.c that converts each number in an array by the sum of that number plus 6 modulus 10. A sample input/output: Enter the length of the array: 5 Enter the elements of the array: 3 928 4 14 77 Output: 9 4 0 0 3 The program should include the following function: void convert(int *a1, int n, int *a2) The function converts every element in array a1 of length n to an output array a2. The...

  • c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detec...

    c++ write a program that reads all values from a text file "data.txt" and stores them in ID array valuesl I. The input process from the file should be terminated when a negative value is detected. (An example of such a file is shown below). Also, the program should copy from values[ 1 any value which has even sum of digits and its index (location) to two new arrays evenArr 1 and idxArrl I, respectively. In addition, the program must...

  • I need a c++ code please. 32. Program. Write a program that creates an integer constant...

    I need a c++ code please. 32. Program. Write a program that creates an integer constant called SIZE and initialize to the size of the array you will be creating. Use this constant throughout your functions you will implement for the next parts and your main program. Also, in your main program create an integer array called numbers and initialize it with the following values (Please see demo program below): 68, 100, 43, 58, 76, 72, 46, 55, 92, 94,...

  • Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a...

    Kindly solve this using C PROGRAMMING ONLY. 4. Write a program marks.c which consists of a main function and three other functions called. readmarks , changemarks (, and writemarks() The program begins by calling the function readmarks () to read the input file marksin. txt which consists of a series of lines containing a student ID number (an integer) and a numeric mark (a float). Once the ID numbers and marks have been read into arrays (by readmarks () the...

  • Write in C++ program Larger than n In a program, write a function that accepts three...

    Write in C++ program Larger than n In a program, write a function that accepts three arguments: an array, the size of the array, and a number n. Assume the array contains integers. The function should display all of the numbers in the array that are greater than the number n. Input from the keyboard: The filename and path of a list of integer numbers.                                           The number n to test the file numbers. Output to the console: The...

  • PLEASE SOLVE USING BASIC C++ CODEING PRINCIPLES PLEASE USE BASIC ARRAY, FUNCTION, AND LOOPING PRINCIPLES PLEASE...

    PLEASE SOLVE USING BASIC C++ CODEING PRINCIPLES PLEASE USE BASIC ARRAY, FUNCTION, AND LOOPING PRINCIPLES PLEASE COMPLETE ALL ASPECTS OF PAPER THANK YOU!!! Objectives To learn to code, compile and run a program containing ARRAYS. . Assignment Plan and code a modular program utilizing arrays. Use at least three functions to solve the problem. Input numbers from a textfile. Input the numbers, one by one. Store the even numbers in one array. Store the odd numbers in a second array....

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT