Question

Can anyone help me with this one? I really appreciate it! Find and fix errors and...

Can anyone help me with this one? I really appreciate it!

Find and fix errors and Simplify the createGroups() function using only one loop instead of two

#include

using namespace std;

const double NO_STU = 40;

void createGroups(const string myClass[], int no_stu, string group1, string group2);
void printList(const string [], int);

int main()
{
int no_stu = 8;
string myClass[NO_STU] = { "Linda", "Bob", "Mary", "Jo", "Tim", "Jamie", "Ann", "Tom" };
string group1[NO_STU % 2];
string group2[NO_STU % 2];

createGroups(no_stu, myClass, group1, group2);
printList(myClass[], no_stu); // print the original list
printList(group1[], no_stu ); // print the first group
printList(group2[], no_stu ); // print the second group
  
return 0;
}

/*~*~*~*~*~*~
This function takes an array of strings and
its number of elements and displays its contents
on the same line
*~*/
void printList(const string list[], int size)
{
for( int i = 0; i <= size; i++ )
{
cout << list[i] << " ";
}
cout << endl;
}

/*~*~*~*~*~*~
This function takes an array of strings and
its number of elements, and populates two arrays
group1 - contains the first half
group2 - contains the second half
*~*/
void createGroups(const string myClass[], int no_stu, string group1[], string group2[])
{
int half = no_stu / 2;
  
for ( int i = 0; i < half; i++)
{
group1[i] = myClass[i];
}
  
for ( int i = half, k = 0; i < no_stu; i++, k++)
{
group2[k] = myClass[i];
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <string>

using namespace std;

const int NO_STU = 40;

void createGroups(const string myClass[], int no_stu, string group1[], string group2[]);

void printList(const string [], int);

int main() {
    int no_stu = 8;
    string myClass[NO_STU] = {"Linda", "Bob", "Mary", "Jo", "Tim", "Jamie", "Ann", "Tom"};
    string group1[NO_STU / 2];
    string group2[NO_STU / 2];

    createGroups(myClass, no_stu, group1, group2);
    printList(myClass, no_stu); // print the original list
    printList(group1, no_stu); // print the first group
    printList(group2, no_stu); // print the second group

    return 0;
}

/*~*~*~*~*~*~
This function takes an array of strings and
its number of elements and displays its contents
on the same line
*~*/
void printList(const string list[], int size) {
    for (int i = 0; i <= size; i++) {
        cout << list[i] << " ";
    }
    cout << endl;
}

/*~*~*~*~*~*~
This function takes an array of strings and
its number of elements, and populates two arrays
group1 - contains the first half
group2 - contains the second half
*~*/
void createGroups(const string myClass[], int no_stu, string group1[], string group2[]) {
    int half = no_stu / 2, k = 0;

    for (int i = 0; i < no_stu; i++) {
        if (i < half)
            group1[i] = myClass[i];
        else
            group2[k++] = myClass[i];
    }
}
Add a comment
Know the answer?
Add Answer to:
Can anyone help me with this one? I really appreciate it! Find and fix errors and...
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
  • hello there. can you please help me to complete this code. thank you. Lab #3 -...

    hello there. can you please help me to complete this code. thank you. Lab #3 - Classes/Constructors Part I - Fill in the missing parts of this code #include<iostream> #include<string> #include<fstream> using namespace std; class classGrades      {      public:            void printlist() const;            void inputGrades(ifstream &);            double returnAvg() const;            void setName(string);            void setNumStudents(int);            classGrades();            classGrades(int);      private:            int gradeList[30];            int numStudents;            string name;      }; int main() {          ...

  • When running the program at the destructor  an exception is being thrown. Can someone help me out?...

    When running the program at the destructor  an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public:    varArray(); // void constructor    int arraySize() const { return size; } // returns the size of the array    int check(double number); // returns index of element containg "number" or -1 if none    void addNumber(double); // adds number to the array    void removeNumber(double); // deletes the number from the array   ...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • c++ /*This is the starter file for your final proficiency test This program has a function...

    c++ /*This is the starter file for your final proficiency test This program has a function that will generate a list of integers using the rand function. Your job is to fill the insertNum and oddCount functions.*/ #include <iostream> #include <ctime> using namespace std; //constants and function prototypes const int CAP = 100; int buildList(int[], int size); void printList(int[], int size); //your functions to implement /*This function finds even numbers in the list, and inserts a number before the even...

  • This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and...

    This project is divided into 3 parts: Part 1. Create a new project and download the arrayList and unorderedArrayList templates that are attached. Create a header file for your unorderedSet template and add it to the project. An implementation file will not be needed since the the new class will be a template. Override the definitions of insertAt, insertEnd, and replaceAt in the unorderedSet template definition. Implement the template member functions so that all they do is verify that the...

  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public:    ...

  • Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8...

    Any help in the compiler error //Driver Program //***************** /** * * CSCI 241 Assignment 8, Part 3 * * Author: your name * z-ID: your z-ID * Date: due date of assignment * * This program builds, sorts and prints lists using the quicksort and * merge sort algorithms. */ #include <iostream> #include <iomanip> #include <vector> #include <string> #include "sorts.h" #include "quicksort.h" #include "mergesort.h" using std::cout; using std::fixed; using std::left; using std::setprecision; using std::string; using std::vector; // Data files...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

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