Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey everyone, thank you for taking the time to review this question. Ive been aw

ID: 3766873 • Letter: H

Question

Hey everyone, thank you for taking the time to review this question. Ive been away from class due to external factors and because of that I am quite behind. I am looking to get the answers/source code and then relearn the material from their. I need this info within 48 hours. Thank you so much! It is imperative that this is written in ***C+***.

Thank you!

This lab is to give you more experience with C++ Searching and Sorting Arrays Given a file with data for names and marks you will read them into two arrays You will then display the data, do a linear search and report if found, sort the data, do a binary search. Be sure to test for found and not found in your main program.

1. Read Data Write a function that reads in data from a file using the prototype below. The function can define and open the file, read the names and marks into the arrays and keep track of how many names there are in numElts, and then closes the file. The list of names and marks can be found at the end of the lab and copied into a text file.
void getNames(string names[], int marks[], int& numElts);

5. Display Data Write a function to display the contents of names and marks using the prototype given below.
void displayData(const string names[], const int marks[], int numElts);

2. Linear Search Write the searchList function given the prototype below so that it searches for a given name. The functions returns and int which is the index of the name found. The main program will decide if -1 is returned then it will say name is not found otherwise it will write out the name and the mark for that name.
int linearSearch(const string list[], int numElts, string value);

3. Selection Sort Write the selectionSort given the prototype below so that it sorts by name in ascending order. Be sure to accept both arrays name and mark for sorting purposes.
void selectionSort(string name[], double mark, int numElts);

4. Binary Search Write the binarySearch function given the prototype below so that it searches for a given name.The functions returns and int which is the index of the name found. The main program will decide if -1 is returned then it will say name is not found otherwise it will write out the name and the mark for that name.
int binarySearch(const string array[], int numElts, string value);

5. Main Function Write a main function that declares the names, marks, number of elements as well as the value to be searched for and the index of the returned function calls.
• Read and display the contents of names and marks.
• Ask the user for a name and using the linear search return the index to the user. If -1 is returned then the name is not in the file. Otherwise write out the name and mark for that student.
• Sort the arrays and write them out.
• Ask the user for a name to search for. This time use the binarySearch to return -1 or an index. Display the student’s name and mark if found.
• Test your program for found and not found for each type of search.
void getNames(string names[], int marks[], int numElts); int linearSearch(const string names[], int numElts,string who); int binarySearch(const string names[], int numElts,string who); void selectionSort(string names[], int marks[],int numElts); void displayData(const string names[], const int marks[], int numElts); const int NUM_Names = 20;
int main()
{
string names[NUM_NAMES]; int marks[NUM_NAMES]; int numElts;
int index; string searchWho;
// Function calls here
return 0;
}
File for names and marks:
Collins,Bill 80
Smith,Bart 75
Allen,Jim 82
Griffin,Jim 55
Stamey,Marty 90
Rose,Geri 78
Taylor,Terri 56
Johnson,Jill 77
Allison,Jeff 45
Looney,Joe 89
Wolfe,Bill 63
James,Jean 72
Weaver,Jim 77
Pore,Bob 91
Rutherford,Greg 42
Javens,Renee 74
Harrison,Rose 58
Setzer,Cathy 93
Pike,Gordon 48
Holland,Beth 79

Explanation / Answer

Introduction And Definition

int mark1, mark2, mark3, ..., mark100;

int mark[100];

7.2    One Dimensional Array: Declaration

array_element_data_type    array_name[array_size];

char   character[20];

int        x[20], y[50];

float     price[10], yield;

char     letter[70];

7.3    Array Initialization

type   array_name[size] = {value_list};

int    id[7] = {1, 2, 3, 4, 5, 6, 7};

float x[5] = {5.6, 5.7, 5.8, 5.9, 6.1};

char   vowel[6] = {'a', 'e', 'i', 'o', 'u', ''};

char    array_name[size] = "string_lateral_constant";

char    vowel[6] = "aeiou";

1.1       Array And Function: Passing One Dimensional Arrays To Function

// pointers, will be explained in another Module

int    myfunction(float *x)

// sized array

char    yourfunction(float x[5])

// unsized array

void    ourfunction(float x[ ])

// function prototype

void func(float *);

                       

int main()

{

      float x[5];

      // an array name (without the bracket) is

      // the pointer to the first array element

      func(x);

      return 0;

}

// function definition

void func(float *pter)

{ return; }

1.2       Array Manipulation: How to use an array and what array can do?

1.2.1           Accessing Array’s Element

// a program to find the total of all the elements in array y

#include <iostream>

using namespace std;

// replace every n occurrences with 7

#define n 7

int main()

{

     int i, total = 0, y[n] = {6,9,2,4,5,23,12};

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

     {

            // display the array contents...

            cout<<y[i]<<" ";

            // do the summing up...

            total = total + y[i];

     }

     // display the result...

     cout<<" Sum of 7 numbers in an array is = "<<total<<endl;

     return 0;

}

Output:

int    get_total(int*, int)

// a program to find the total values of an

// array y by passing an array to a function using pointer

#include <iostream>

using namespace std;

#define n 7

// function prototype

int get_total(int*, int);

int main()

{

     int total, y[n]={6,9,2,4,5,23,12};

     cout<<" Calling function get_total(y, n),";

     cout<<" By bringing along the value of y, an array";

     cout<<" first address and n = 7, an array size.";

     cout<<" An array name, is the pointer to the";

     cout<<" 1st element of an array ";

     // a function call, pass along the pointer to the first

     // array element and the array size, and the

     // return result assign to variable total

     total = get_total(y, n);

     cout<<" Sum of the 7 array elements is "<<total<<endl;

     return 0;

}

               

// function definition

int get_total(int *ptr, int x)

{

     int i, total = 0;

     // do the looping for array elements...

     for(i=0; i<x; i++)

     {

            // displays the array content, pointed by pointer...

            cout<<*(ptr+i)<<" ";

            // do the summing up of the array elements...

            total += *(ptr+i); // total=total + *(ptr+i);

     }

     // return the result to the calling program...

     return total;

}

Output:

---------------------------------------------------------------------------------------------------------------

// **********gccarray.C or gccarray.cpp************

// ************FeDoRa 3, g++ x.x.x**********

// a program to find the total values of an

// array y by passing an array to a function using pointer

#include <iostream>

#define n 7

using namespace std;

// a function prototype

int get_total(int*, int);

int main()

{

        int total, y[n]={6,9,2,4,5,23,12};

        cout<<" Calling function get_total(y, n),";

        cout<<" By bringing along the value of y, an array";

        cout<<" first address and n = 7, an array size.";

        cout<<" An array name, is the pointer to the";

        cout<<" 1st element of an array ";

        // function call, pass along the pointer to the first

        // array element and the array size, and the

        // return result assign to variable total

        total = get_total(y, n);

        cout<<" Sum of the 7 array elements is "<<total<<endl;

        return 0;

}

// a function definition

int get_total(int *ptr, int x)

{

        int i, total = 0;

        // do the looping for array elements...

        for(i=0; i<x; i++)

        {

                // displays the array content, pointed by pointer...

                cout<<*(ptr+i)<<" ";

                // do the summing up of the array elements...

                total += *(ptr+i); //total=total + *(ptr+i);

        }

        // return the result to the calling program...

        return total;

}

[bodo@bakawali ~]$ g++ gccarray.C -o gccarray

[bodo@bakawali ~]$ ./gccarray

Calling function get_total(y, n),

By bringing along the value of y, an array

first address and n = 7, an array size.

An array name, is the pointer to the

1st element of an array

6 9 2 4 5 23 12

Sum of the 7 array elements is 61

[bodo@bakawali ~]$

7.5.2    Searching For A Value

// a program to find the smallest number in an array

// named balance, a very simple search function

#include <iostream>

using namespace std;

#define n 10

int main()

{

     int i;

     float small, balance[n]={100.00,40.00,-30.00,400.00,60.00,-25.00,-24.00,0.00,3.24,0.50};

     small = balance[0];

     // loop for displaying array content....

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

         cout<<balance[i]<<" ";

     // another loop do the array element comparing...

     for(i=1; i<n; i++)   // check until condition i = n

     {

            if(small > balance[i])

                small = balance[i];

    }

     cout<<" Searching..."<<endl;

     // display the result...

     cout<<"The smallest value in the given array is = "<<small<<endl;

     return 0;

}

Output:

1.1.1           Exchanging Values of Variables

// assign num1 to third_var

third_var = num1;

// then assigns num2 to num1

num1 = num2;

// finally assigns third_var to num2

num2 = third_var;

1.1.2           Sorting Variables

// a simple sorting program that sort a list of n

// integer numbers, entered by the user, ascendingly

#include <iostream>

using namespace std;

#define   maxsize   100

int main()

{

     int temp, i, j, n, list[maxsize];

     cout<<" --You are prompted to enter your list size.--";

     cout<<" --Then, for your list size, you are prompted to enter--";

     cout<<" --the element of your list.--";

     cout<<" --Finally your list will be sorted ascending!!!-- ";

     // get the list size...

     cout<<" Enter your list size: ";

     cin>>n;

     // prompting the data from user and store it in the list...

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

     {

            cout<<"Enter list's element #"<<i<<"-->";

            cin>>list[i];

     }

    

     // do the sorting...

     for(i=0; i<n-1; i++)

            for(j=i+1; j<n; j++)

                if(list[i] > list[j])

                {

                     // these three lines swap the elements list[i] and list[j].

                     temp = list[i];

                     list[i] = list[j];

                     list[j] = temp;

                }

     cout<<" Sorted list, ascending: ";

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

            cout<<" "<<list[i];

    cout<<endl;

                   return 0;

}

Output:

1.2     Two Dimensional/2D Arrays

data_type    array_name[1st dimension size][2nd dimension size];

int        x[3][4];

float    matrix[20][25];

For n rows and m columns, the total size equal to mn

// a 2D array that can store 4 names, each is 10 characters long

char    name[4][10];

int   x[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};

x[0][0]=1       x[0][1]=2      x[0][2]=3     x[0][3]=4

x[1][0]=5       x[1][1]=6      x[1][2]=7     x[1][3]=8

x[2][0]=9       x[2][1]=10     x[2][2]=11    x[2][3]=12

int x[3][4] = {{1,2,3,4},

         {5,6,7,8},

         {9,10,11,12}

         };

// all array elements will be 0

int   x[3][4]={0};

char   name[4][10] = {"Sally", "Joyce", "Lisa", "Alice"};

name[0] = "Sally"       name[1] = "Joyce"

name[2] = "Lisa"        name[3] = "Alice"

Introduction And Definition

  • Up till this Module we were introduced with simple data types that suit to simple applications. For aggregated data, with same type of element we can use Array.
  • An array in C / C++ is a collection of related data elements of the same type that are referenced by a common name. Generally, it is just another data type, aggregate data type.
  • All the elements of an array occupy a set of contiguous memory locations and by using an index or subscript we can identify each element.
  • For example, instead of declaring mark1, mark2, ..., markN to store and manipulate a set of marks obtained by the students in certain courses, we could declare a single array variable named mark and use an index, such as j, to refer to each element in mark. This absolutely has simplified our declaration of the variables.
  • Hence, mark[ j ] would refer to the jth element in the array mark. Thus by changing the value of j, we could refer to any element in the array, so it simplifies our declaration.
  • For example, if we have 100 list of marks of integer type, we will declare it as follows:

int mark1, mark2, mark3, ..., mark100;

  • If we have 100 marks to be stored, you can imagine how long we have to write the declaration part by using normal variable declaration?
  • By using an array, we just declare like this:

int mark[100];

  • This will reserve 100 contiguous/sequential memory locations for storing the integer data type.
  • Graphically can be depicted as follows: