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

Write code in C++ Objective: In this assignment, you need to mainly demonstrate

ID: 3767079 • Letter: W

Question

Write code in C++

Objective: In this assignment, you need to mainly demonstrate your ability in using pointers with basic data types.

Description
Assume that you manage online shopping portal and you have customers only from two states: T and O short for Texas and Oklahoma, respectively.  In your program first you randomly create N number of customers. You will ask user to enter N, which is a positive integer number between 10 and 1000.  Each customer has these characteristics.

Customer ID:  a positive integer number between 5000 and 10000.

Customer First initial: a char between A and Z

Customer Last initial: a char between A and Z

Customer State: a char, Either T or O

Customer Zip: a positive integer number,

                            between 70000 and 74999 if customer live in T

between 75000 and 79999 if customer live in O

Based on given data field, you will define your struct first. Then dynamical create an array for N number of costumers.  This array is referred as “main” array.

The main aim of this program is to build two dynamic arrays  for Texas and Oklahoma customer based on main array with N items.

Assume that N is entered as 500. You randomly built your main array in which you created 210 Texas customer, and 290 Oklahoma customers. Then you will two dynamic customer arrays to keep Texas and Oklahoma customer separately. The size of T array will be 210, and O array will be 290. Once you created your arrays, customers will be copied from main array to their corresponding arrays, i.e., all Texas customers of main array will be copied to Texas array, and all Oklahoma customers will be copied to Oklahoma array.

At the end of your program, you should print out all customer data from Texas and Oklahoma arrays separately.

Explanation / Answer

RUN THIS LIKE:

g++ <filename.cc> -std=c++11

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

#include <iostream>
#include <random>
using namespace std;
struct Customer
{
   int customerId;
   char firstInitial;
   char lastInitial;
   char state;
   int zip;
  
};
int main() {
   ios::sync_with_stdio(false);
   int N, stateCode, countTexasCustomers = 0, countOklahomacustomers = 0;
   bool flag = true;
   while (flag) {
       cout << "Enter number of customers. (Must be an integer between 10 and 1000" << endl;
       cin >> N;
       if (N >= 10 && N <= 1000)
           flag = false;
       else
           cout << "Number of customers is out of range" << endl;

   }
   Customer **main ;
   main = new Customer *[N];
   random_device rd; // obtain a random number from hardware
mt19937 eng(rd()); // seed the generator
uniform_int_distribution<> generateId(5000, 10000); // define the range for customer id
uniform_int_distribution<> generateFirstInitial(1, 26); // define the range for first initial name
uniform_int_distribution<> generateLastInitial(1, 26); // define the range for last initial name
uniform_int_distribution<> generateState(1, 2); // define the range for state
uniform_int_distribution<> generateTZip(70000, 74999); // define the range for texas zip
uniform_int_distribution<> generateOZip(75000, 79999); // define the range for oklahoma zip


for(int i = 0; i < N; ++i) {
   main[i] = new Customer;
   main[i]->customerId = generateId(eng);
   main[i]->firstInitial = generateFirstInitial(eng) + 64;
   main[i]->lastInitial = generateLastInitial(eng) + 64;
   stateCode = generateState(eng);
   if (stateCode == 1) {
       countTexasCustomers++;
       main[i]->state = 'T';
       main[i]->zip = generateTZip(eng);
   }
   else if (stateCode == 2) {
       countOklahomacustomers++;
       main[i]->state = 'O';
       main[i]->zip = generateOZip(eng);
   }
}
Customer **texas, **oklahoma;
texas = new Customer *[countTexasCustomers];
oklahoma = new Customer *[countOklahomacustomers];

int countT = 0, countO = 0;
for (int i = 0; i < N; i++) {
   if (main[i]->state == 'T') {
       texas[countT] = new Customer;
       texas[countT++] = main[i];
   }
   else if (main[i]->state == 'O') {
       oklahoma[countO] = new Customer;
       oklahoma[countO++] = main[i];
   }
}

cout << "Information of Texas customers" << endl;
for (int i = 0; i < countTexasCustomers; i++) {
   cout << texas[i]->customerId << " " << texas[i]->firstInitial << " " << texas[i]->lastInitial << " " << texas[i]->state << " " << texas[i]->zip << endl;
}

cout << "Information of Oklahoma customers" << endl;
for (int i = 0; i < countOklahomacustomers; i++) {
   cout << oklahoma[i]->customerId << " " << oklahoma[i]->firstInitial << " " << oklahoma[i]->lastInitial << " " << oklahoma[i]->state << " " << oklahoma[i]->zip << endl;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote