Question

In: Computer Science

Using C++ Use dynamic allocation and functions (using pointer variables only) to read the names from...

Using C++ Use dynamic allocation and functions (using pointer variables only) to read the names from the .txt file and sort the names in lexical order

Grumpy

Dopey

Doc

Happy

Bashful

Sneezy

Sleepy

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

#include <fstream>
#include <iostream>
#include <cstring>

using namespace std;

void sort(string names[], int count);
int main()
{
   // Declaring variables
string* names = NULL;
  
int count = 0;
string name;
ifstream dataIn;
  
// open the input file
dataIn.open("strnames.txt");
/*
* checking whether the file name is valid or not
*/
if (dataIn.fail())
{
cout << "** File Not Found **";
return 1;
}
else
{
/*
* Reading the data from the file
*/
while (dataIn >> name)
{
count++;
}
/*
* closing the file
*/
dataIn.close();

// Creating array dynamically
names = new string[count];

dataIn.open("strnames.txt");
for (int i = 0; i < count; i++)
{
dataIn >> names[i];
}
dataIn.close();

cout << "Before Sorting :" << endl;
for (int i = 0; i < count; i++)
{
cout << names[i] << endl;
}
sort(names, count);
cout << "After Sorting :" << endl;

for (int i = 0; i < count; i++)
{
cout << names[i] << endl;
}
}

return 0;
}

// this function will sort the string in lexical order
void sort(string names[], int size_of_arr)
{
string temp;
for (int i = 0; i < size_of_arr - 1; i++)
{
for (int j = 0; j < size_of_arr - i - 1; j++)
{
if (names[j] > names[j + 1])
{
temp = names[j];
names[j] = names[j + 1];
names[j + 1] = temp;
}
}
}
}

==========================================

==============================================

Output:

=====================Could you plz rate me well.Thank You


Related Solutions

Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
In the following keypad notation Use a class and dynamic allocation of a pointer variable to...
In the following keypad notation Use a class and dynamic allocation of a pointer variable to enter the digit code of the following text input MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY Use an inherited class using pointer variable to output the phone number from the following text input 1-800-COMCAST 1-800-VERIZON 1-800-BANCORP 1-800-MYKOHLS 1-800-JCPENNY Write C++ code and pseudocode in a doc file A computer key board has defect (like speech defect in humans) in reading for ‘p’ /’P’ as...
In the following keypad notation Use a class and dynamic allocation of a pointer variable to...
In the following keypad notation Use a class and dynamic allocation of a pointer variable to enter the digit code of the following text input MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY Use an inherited class using pointer variable to output the phone number from the following text input 1-800-COMCAST 1-800-VERIZON 1-800-BANCORP 1-800-MYKOHLS 1-800-JCPENNY Write C++ code and pseudocode in a doc file A computer key board has defect (like speech defect in humans) in reading for ‘p’ /’P’ as...
When it comes to dynamic memory allocation and delete[] pointer, what does it mean to say...
When it comes to dynamic memory allocation and delete[] pointer, what does it mean to say that, CRT detected that the application wrote to memory after end of heap buffer? I tried to duplicate the error by deleting the pointer address twice and it didn't produce the same error code so that's not it. What might cause this problem?
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files...
Objectives:  Write classes in C++  Use dynamic arrays  Write and read from files 1. WriteaclassGradeBookcontainingthefollowing: Private attributes: - courseName: a string representing the name of the course. - nbOfStudents: an integer representing the number of students enrolled in the course. The number of students is greater than or equal to 5. - grades: a double dimensional array of integers representing the grades of Test1, Test2 and Final of every student. It should be a dynamic array. Public...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable (like ptr) which points to this array. Use this pointer variable to initialize the myNums array from 2 to 200 and then display the array elements. Delete the dynamic array myNums at the end. You just need to write part of the program.
Purpose Review and reinforcement of pointers, dynamic memory allocation, pointer arithmetic, passing pointers to a function,...
Purpose Review and reinforcement of pointers, dynamic memory allocation, pointer arithmetic, passing pointers to a function, returning a pointer by a function, dangling pointer, and memory deallocation, pointer initialization, and struct data type. Project description In this project, you will create a database of employees of an organization while meeting the requirements described below. Your program MUST NOT interact with the user to receive inputs, so that the instructor and/or the teaching assistant can save a big time in testing...
1) Dynamic Allocation (c++) a. Create two integer variables x and y, initialize them with different...
1) Dynamic Allocation (c++) a. Create two integer variables x and y, initialize them with different values. b. Use dynamic memory allocation, declare px and py as address of x and y separately. c. Print out x, y, px, py, &x, &y, *px, *py.   d. Let py = px, and *py = 100 e. Print out x, y, px, py, &x, &y, *px, *py. g. Print out *px++, x, px
Write a C++ program using dynamic arrays that allows the user to enter the last names...
Write a C++ program using dynamic arrays that allows the user to enter the last names of the candidates in a local election and the number of votes received by each candidate. The program must ask the user for the number of candidates and then create the appropriate arrays to hold the data. The program should then output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT