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

Assume that you manage online shopping portal and you have customers only from t

ID: 3766660 • Letter: A

Question

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

code:

#include <bits/stdc++.h>
using namespace std;

struct Customer{
   int id;
   char first;
   char last;
   char state;
   int zip;
   Customer(){
       id=0;
       first=' ';
       last=' ';
       state=' ';
       zip=0;
   }
};

char c_state(int i){
   if(i==0){
       return 'T';
   }
   else{
       return 'O';
   }
}

int c_zip(char s){
   if(s=='T'){
       return 70001+rand()%4998;
   }
   else{
       return 75001+rand()%4998;
   }
}

int main(){
   srand(time(NULL));
   int n;
   cout<<"Please enter a numbet between 10 and 1000 : ";
   cin>>n;
   Customer *mainarr=new Customer[n];
   int tex=0;
   int okl=0;
   for(int i=0;i<n;i++){
       int cus_id=5001+rand()%4999;
       char f=char((rand()%26)+65);
       char l=char((rand()%26)+65);
       char s=c_state(rand()%2);
       int z=c_zip(s);
       Customer c;
       c.id=cus_id;
       c.first=f;
       c.last=l;
       c.state=s;
       c.zip=z;
       if(s=='T'){
           tex++;
       }
       else{
           okl++;
       }
       mainarr[i]=c;
   }
   Customer *texarr=new Customer[tex];
   Customer *oklarr=new Customer[okl];
   int count1=0;
   int count2=0;
   for(int i=0;i<n;i++){
       if(mainarr[i].state=='T'){
           texarr[count1]=mainarr[i];
           count1++;
       }
       else{
           oklarr[count2]=mainarr[i];
           count2++;

       }
   }
   for(int i=0;i<tex;i++){
       cout<<texarr[i].id<<" "<<texarr[i].first<<" "<<texarr[i].last<<" "<<texarr[i].state<<" "<<texarr[i].zip<<endl;
   }
   for(int i=0;i<okl;i++){
       cout<<oklarr[i].id<<" "<<oklarr[i].first<<" "<<oklarr[i].last<<" "<<oklarr[i].state<<" "<<oklarr[i].zip<<endl;
   }

   return 0;
}