PropellerAds

Showing posts with label program. Show all posts

An assignment of nust on pointer in C++. copying, concatinating and searching string using pointers

1.       Create a menu with following options
a.       Give Input
                                                               i.      Takes input from user and saves it in the first array
b.      Copy a string
                                                               i.      User will enter a string and it will be copied in both the arrays
c.       Concat string
                                                               i.      When concat string option is selected, concatenate the string in the first array to second array
d.      Search String
                                                               i.      Search first array for the string entered by the user and only print true/false if found/not found
e.      Display String
                                                               i.      Display the second array to user when this option is selected.
2.       For above options your program will have six functions in total including main.
3.       Create two character arrays of size 1000 in main function.
4.       Every time user selects an option pass both the arrays to functions in the form of pointers.
5.       Use pointers to traverse and manipulate the arrays.
6.       Create a temporary array inside any function is allowed.


   
//C++ program to copy cancatenate search and display string through pointers
#include<iostream>
#include<conio.h>


using namespace std;

const int array_size = 1000;

void input(char *a);
void copy(char *a, char *b);
void concat(char *a, char *b);
int search(char *a, char *b);
void display(char *a, char *b);

void main(){
char arr1[array_size];
char arr2[array_size];
char a;
int b = 0;

//initialinzing array
arr1[0] = '\0';
arr2[0] = '\0';
for (int i = 1; i < array_size; i++){
arr1[i] = '-';
arr2[i] = '-';
}

while (true){
cout << "Enter the respective key to perform function:" << endl << endl
<< "1.\tInput String." << endl
<< "2.\tCopy String." << endl
<< "3.\tConcat String." << endl
<< "4.\tSearch String." << endl
<< "5.\tDisplay String." << endl;

a = _getch();

system("cls");
switch (a)
{
case '1':
input(arr1);
break;
case '2':
copy(arr1, arr2);
break;
case '3':
concat(arr1, arr2);
break;
case '4':
b = search(arr1, arr2);
if(b == 0){
cout << arr1 <<" String not founded";
} else{
cout << arr1 << " founded " << b << " times.";
}
break;
case '5':
display(arr1, arr2);
break;
default:
system("cls");
cout << "Invalid Key" << endl;
break;
}


cout << "\n\nPress any key to continue.....";
_getch();
system("cls");
}
}

void input(char *a) {
cout << "Enter string:";
cin >> a;

cout << "Successfully Entered...";
}

void copy(char *a, char *b){
cout << "Copying string....";
while (*a != '\0'){//lastpoint of copiying string
if (*b == '-' || *b == '\0'){//last point of 2nd string or its empty slots
*b = *a; //copy value
a++; //move to next value
}
b++;

if (*a == '\0'){            //end of copying string
*b = '+';
b++;
*b = '\0';
}
}
}
void concat(char *a, char *b){
char *c = a;
cout << "Concatinating string....";
while (*c != '\0'){
if (*b == '-' || *b == '\0'){
*b = *c;
c++;
}
b++;

if (*c == '\0'){
*b = '+';
b++;
*b = '\0';
}

}

*a = '\0';

}
int search(char *a, char *b){
char *c = a;
int counter = 0;

cout << "Searching: " << c << endl << endl;

c= a;

while(*b != '\0'){

while(*b != '+'){
if(*c == *b){
c++;
b++;
} else{
c = a;
b++;
}
if(*c == '\0' && *b == '+'){
counter++;
}
}

b++;
c = a;
}

if(counter > 0){return counter;}

return 0;
}
void display(char *a, char *b){
cout << "String A: " << a << endl;
cout << "String B: " << b << endl;
}

An assingment of nust. Bill payment solution.

Problem:

You have to serve 20 people in all.

  •  space available in queue is for 10 people.
  •  first person entering the queue will be served first.
  •  people coming to pay the bill will be added 
  • if yes (ask for amount)
  • if no move next
  • In every iteration you will ask the secrity guard if any person is served or not
  • if yes ask number of user served and process them
  • when every one is served print the total amount of payment recieved
    Solution:

      #include<iostream>
        #include<conio.h>


            using namespace std;


                void enter_queue(int billing_array[], int &persons_in_queue, int &total_person_served){


                    char security = 'n';


                        if (total_person_served + persons_in_queue < 20){
                          cout << "To security guard:" << endl << "Is there any person to enter in queue? (press y for yes): ";
                            cin >> security;

                                if (security == 'y' && persons_in_queue < 10){
                                  do{
                                    cout << "Enter in queue, show your bill to security guard: ";
                                      cin >> billing_array[persons_in_queue];
                                        if (billing_array[persons_in_queue] < 0){
                                          cout << "Wrong Input" << endl;
                                            }
                                              } while (billing_array[persons_in_queue] < 0);
                                                persons_in_queue++;
                                                  } else if (persons_in_queue >= 10){
                                                    cout << endl << "No more space in queue....." << endl;
                                                      }
                                                        }
                                                          }


                                                              void people_served(int billing_array[], int &persons_in_queue, int &total_payment_recieved, int &total_person_served){

                                                                  int person_served = 0;
                                                                    cout << "To security guard:\nHow many user are served?\nNumber of user served are: ";
                                                                      cin >> person_served;


                                                                          //readjust/serve the person;
                                                                            if (person_served <= persons_in_queue){
                                                                              for(int i = 0; i < person_served; i++){
                                                                                total_payment_recieved += billing_array[i];
                                                                                  billing_array[i] = billing_array[i+person_served];
                                                                                    persons_in_queue--;
                                                                                      }

                                                                                          for(int i = persons_in_queue; i < 10; i++){
                                                                                            billing_array[i] = 0;
                                                                                              }
                                                                                                total_person_served += person_served;
                                                                                                  } else{
                                                                                                    cout << "Person served Exceeds person in queue limit..." << endl;
                                                                                                      }

                                                                                                          }


                                                                                                              void main(){
                                                                                                                int billing_array[10] = {0};
                                                                                                                  int persons_in_queue = 0;
                                                                                                                    int total_payment_recived = 0;
                                                                                                                      int total_person_served = 0;


                                                                                                                          while(total_person_served < 20){
                                                                                                                            //entry point queue;
                                                                                                                              enter_queue(billing_array, persons_in_queue, total_person_served);
                                                                                                                                people_served(billing_array, persons_in_queue, total_payment_recived, total_person_served);


                                                                                                                                    //print queue;
                                                                                                                                      cout << endl << "Queue status;" << endl;
                                                                                                                                        for (int i = 0; i < 10; i++){
                                                                                                                                          cout << billing_array[i] << endl;
                                                                                                                                            }


                                                                                                                                                cout << endl;
                                                                                                                                                  }


                                                                                                                                                      cout << endl << "Total Payment Recived: " << total_payment_recived;


                                                                                                                                                          _getch();
                                                                                                                                                            }
                                                                                                                                                            Tuesday, 27 January 2015
                                                                                                                                                            Posted by Unknown

                                                                                                                                                            Program/code to swap two numbers in C language

                                                                                                                                                            This code demonstrate the swaping of two numbers.

                                                                                                                                                            //program to swap two numbers input by user
                                                                                                                                                            #include<stdio.h>
                                                                                                                                                            #include<conio.h>
                                                                                                                                                            void main()
                                                                                                                                                            {
                                                                                                                                                            int x,y,a;

                                                                                                                                                            printf("Enter x:");
                                                                                                                                                            scanf("%d",&x);
                                                                                                                                                            printf("Enter y:");
                                                                                                                                                            scanf("%d",&y);
                                                                                                                                                            a=x;
                                                                                                                                                            x=y; 
                                                                                                                                                            y=a;
                                                                                                                                                            printf("x is %d\n",x);
                                                                                                                                                            printf("y is %d",y);
                                                                                                                                                            getch();
                                                                                                                                                            }
                                                                                                                                                            Saturday, 17 January 2015
                                                                                                                                                            Posted by Unknown
                                                                                                                                                            PropellerAds
                                                                                                                                                            PropellerAds

                                                                                                                                                            Popular Post

                                                                                                                                                            Powered by Blogger.

                                                                                                                                                            - Copyright © BSCS -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -