Showing posts with label programming. 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;
}
Thursday, 29 January 2015
Posted by Unknown
Tag :
array,
c++,
code,
coding,
concatinate,
copying,
pointers,
program,
programming,
project,
project c++(basic),
string
An assingment of nust. Bill payment solution.
Problem:
You have to serve 20 people in all.
#include<iostream>
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
#include<iostream>
Tuesday, 27 January 2015
Posted by Unknown
Program, code to multiply 2 matrix using 1-d array in c++
#include<iostream>
#include<conio.h>
using namespace std;
//Entr order of matrixes
void order(int &matrix1_rows, int &matrix1_colomn, int &matrix2_rows,int &matrix2_colomn) {
cout << "Enter no of rows of 1st Matrix: ";
cin >> matrix1_rows;
cout << "Enter no of colomn of 1st Matrix: ";
cin >> matrix1_colomn;
cout << endl;
cout << "Enter no of rows of 2nd Matrix: ";
cin >> matrix2_rows;
cout << "Enter no of colomn of 2nd Matrix: ";
cin >> matrix2_colomn;
cout << endl;
}
//chk if multiplication is possible
int chk(int matrix1_rows,int matrix1_colomn, int matrix2_rows, int matrix2_colomn){
if(matrix1_colomn == matrix2_rows){
cout << "Multiplication possible" << endl;
return 1;
} else {
cout << "Multiplication not possible" << endl << "Multiply any other Matrix" << "\n\n";
}
return 0;
}
void main() {
//variable for Matrix1
int matrix1_rows = 0;
int matrix1_colomn = 0;
int matrix1[100];
int matrix1_max = 0;
//variable for Matrix2
int matrix2_rows = 0;
int matrix2_colomn = 0;
int matrix2[100];
int matrix2_max = 0;
//variable used to count the matrix element in any loop
int displayCounter1 = 0;
int displayCounter2 = 0;
//variable for result matrix
int result_matrix[100];
int result_matrix_rows = 0;
int result_matrix_colomn = 0;
int result_max;
//variable used for calculation of result
int resultCounter = 0;
int x = 0;
int y = 0;
int z = 0;
//start
cout << "Program to multiply two matrix" << endl << endl;
//retaking user input if multiplication is not possible
int a = 0;
do{
order(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
a = chk(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
} while(a !=1);
//Intializing data if multiplication is possible
matrix1_max = matrix1_rows*matrix1_colomn;
matrix2_max = matrix2_rows*matrix2_colomn;
//data entry matrix1
for(int i=0; i < matrix1_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix1: ";
cin >> matrix1[i];
}
cout << endl;
//data entry matrix2
for(int i=0; i < matrix2_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix2: ";
cin >> matrix2[i];
}
//matrix1 display
cout << "Matrix-1" << endl;
while(displayCounter1< matrix1_max){
cout << "|\t";
for(int i =0; i<matrix1_colomn; i++){
cout << matrix1[displayCounter1] << "\t";
displayCounter1++;
}
cout << '|' << endl;
}
//print multiplication sybmbol between middle
cout << endl << "\tX" << endl;
//matrix2 display
cout << endl << "Matrix-2" << endl;
while(displayCounter2< matrix2_max){
cout << "|\t";
for(int i =0; i<matrix2_colomn; i++){
cout << matrix2[displayCounter2] << "\t";
displayCounter2++;
}
cout << '|' << endl;
}
//intilizing result matrix
result_matrix_rows = matrix1_rows;
result_matrix_colomn = matrix2_colomn;
result_max = result_matrix_rows*result_matrix_colomn;
resultCounter = 0;
//intializing array 0 value
while(resultCounter < result_max){
result_matrix[resultCounter] = 0;
resultCounter++;
}
resultCounter = 0;
//The main multiplication
for(int i = 0; i < matrix1_rows; i++){
for(int j = 0; j <= matrix2_colomn-1; j++){
for(int k = 0; k < matrix1_colomn; k++){
result_matrix[resultCounter] += ((matrix1[k+x]) * (matrix2[z+y]));
y += matrix2_colomn;
}
z++;
y = 0;
resultCounter++;
}
x += matrix1_colomn;
z = 0;
}
//diplaying result
resultCounter = 0;
cout << endl << "Result";
while(resultCounter < result_max){
cout << "|\t";
for(int i =0; i<result_matrix_colomn; i++){
cout << result_matrix[resultCounter] << "\t";
resultCounter++;
}
cout << '|' << endl;
}
_getch();
}
#include<conio.h>
using namespace std;
//Entr order of matrixes
void order(int &matrix1_rows, int &matrix1_colomn, int &matrix2_rows,int &matrix2_colomn) {
cout << "Enter no of rows of 1st Matrix: ";
cin >> matrix1_rows;
cout << "Enter no of colomn of 1st Matrix: ";
cin >> matrix1_colomn;
cout << endl;
cout << "Enter no of rows of 2nd Matrix: ";
cin >> matrix2_rows;
cout << "Enter no of colomn of 2nd Matrix: ";
cin >> matrix2_colomn;
cout << endl;
}
//chk if multiplication is possible
int chk(int matrix1_rows,int matrix1_colomn, int matrix2_rows, int matrix2_colomn){
if(matrix1_colomn == matrix2_rows){
cout << "Multiplication possible" << endl;
return 1;
} else {
cout << "Multiplication not possible" << endl << "Multiply any other Matrix" << "\n\n";
}
return 0;
}
void main() {
//variable for Matrix1
int matrix1_rows = 0;
int matrix1_colomn = 0;
int matrix1[100];
int matrix1_max = 0;
//variable for Matrix2
int matrix2_rows = 0;
int matrix2_colomn = 0;
int matrix2[100];
int matrix2_max = 0;
//variable used to count the matrix element in any loop
int displayCounter1 = 0;
int displayCounter2 = 0;
//variable for result matrix
int result_matrix[100];
int result_matrix_rows = 0;
int result_matrix_colomn = 0;
int result_max;
//variable used for calculation of result
int resultCounter = 0;
int x = 0;
int y = 0;
int z = 0;
//start
cout << "Program to multiply two matrix" << endl << endl;
//retaking user input if multiplication is not possible
int a = 0;
do{
order(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
a = chk(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
} while(a !=1);
//Intializing data if multiplication is possible
matrix1_max = matrix1_rows*matrix1_colomn;
matrix2_max = matrix2_rows*matrix2_colomn;
//data entry matrix1
for(int i=0; i < matrix1_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix1: ";
cin >> matrix1[i];
}
cout << endl;
//data entry matrix2
for(int i=0; i < matrix2_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix2: ";
cin >> matrix2[i];
}
//matrix1 display
cout << "Matrix-1" << endl;
while(displayCounter1< matrix1_max){
cout << "|\t";
for(int i =0; i<matrix1_colomn; i++){
cout << matrix1[displayCounter1] << "\t";
displayCounter1++;
}
cout << '|' << endl;
}
//print multiplication sybmbol between middle
cout << endl << "\tX" << endl;
//matrix2 display
cout << endl << "Matrix-2" << endl;
while(displayCounter2< matrix2_max){
cout << "|\t";
for(int i =0; i<matrix2_colomn; i++){
cout << matrix2[displayCounter2] << "\t";
displayCounter2++;
}
cout << '|' << endl;
}
//intilizing result matrix
result_matrix_rows = matrix1_rows;
result_matrix_colomn = matrix2_colomn;
result_max = result_matrix_rows*result_matrix_colomn;
resultCounter = 0;
//intializing array 0 value
while(resultCounter < result_max){
result_matrix[resultCounter] = 0;
resultCounter++;
}
resultCounter = 0;
//The main multiplication
for(int i = 0; i < matrix1_rows; i++){
for(int j = 0; j <= matrix2_colomn-1; j++){
for(int k = 0; k < matrix1_colomn; k++){
result_matrix[resultCounter] += ((matrix1[k+x]) * (matrix2[z+y]));
y += matrix2_colomn;
}
z++;
y = 0;
resultCounter++;
}
x += matrix1_colomn;
z = 0;
}
//diplaying result
resultCounter = 0;
cout << endl << "Result";
while(resultCounter < result_max){
cout << "|\t";
for(int i =0; i<result_matrix_colomn; i++){
cout << result_matrix[resultCounter] << "\t";
resultCounter++;
}
cout << '|' << endl;
}
_getch();
}
Monday, 22 December 2014
Posted by Unknown
Coing to Generate prime Number in c
#include<stdio.h>;
#include<conio.h>;
//code by waqas javed //main
void main() {
//initializing vars
int i = 1; //prime number founded
int j = 2; //chk start point
int l = 1; //var factor chk var
int m = 0; //number of factor
int number = 0;
//user tell how many prime number to generate
printf("Enter how many prime number you want to generate: ");
scanf("%d", &number);
//printing first 20 prime no.
while (i <= number){
//chk for factors of number
while (j > l){
//if number is factor
if (j%l == 0){
m++;
l++;
}
//if not
else {
l++;
}
}
//chk and print if prime no.
if (j == l) {
//if prime
if (m == 1)
printf("%d.\t%d \n", i++, j);
j++;
l = 1;
m = 0;
}
//if not reinitializing value to chk nxt var
else {
j++;
l = 1;
m = 0;
}
}
_getch();
}
This code work as it starts from 2 and check whether it is a even number or not.. Its checking is designed like the program checks all the number below that number to be checked that whether they are its factor are not... Then the program check how many factor the number has if they are 2 then it take it as prime number and print it..
For example: Program is checking 9 is a prime no or not. it will check all number below it that is 1,2,3,4,5,6,7,8 now the we have 1 and 3 as its factor and 9 itself also so total factor exceeded 2 and the number is not prime...... similarly it will keep checking all number in increasing order starting from 1 until the total prime number too be founded are found.
#include<conio.h>;
//code by waqas javed //main
void main() {
//initializing vars
int i = 1; //prime number founded
int j = 2; //chk start point
int l = 1; //var factor chk var
int m = 0; //number of factor
int number = 0;
//user tell how many prime number to generate
printf("Enter how many prime number you want to generate: ");
scanf("%d", &number);
//printing first 20 prime no.
while (i <= number){
//chk for factors of number
while (j > l){
//if number is factor
if (j%l == 0){
m++;
l++;
}
//if not
else {
l++;
}
}
//chk and print if prime no.
if (j == l) {
//if prime
if (m == 1)
printf("%d.\t%d \n", i++, j);
j++;
l = 1;
m = 0;
}
//if not reinitializing value to chk nxt var
else {
j++;
l = 1;
m = 0;
}
}
_getch();
}
This code work as it starts from 2 and check whether it is a even number or not.. Its checking is designed like the program checks all the number below that number to be checked that whether they are its factor are not... Then the program check how many factor the number has if they are 2 then it take it as prime number and print it..
For example: Program is checking 9 is a prime no or not. it will check all number below it that is 1,2,3,4,5,6,7,8 now the we have 1 and 3 as its factor and 9 itself also so total factor exceeded 2 and the number is not prime...... similarly it will keep checking all number in increasing order starting from 1 until the total prime number too be founded are found.
Posted by Unknown
Lets Get Started with programming.
Writing your
first program in C/C++
According to me programming is not something to learn it
comes to you automatically by practicing. Writing program is more affectionate
in this respect then becoming bookish.
So here is your first program to start with try writing it
and understanding it.
1. // my first
program in C
2. #include <stdio.h>
3. void
main () {
4. printf("My
first program");
5. }
1. // my
first program in C++
2. #include <iostream>
3. using namespace std;
4. void
main () {
5. cout <<
"My first program";
6. }
OUTPUT PANEL
My first program
Explaining of program in C
Line
1: // my first program in C++
// indicates the start of comment all
the text written in the line next too this will be ignored as programming line
and itt will juxt be added in programming as it is. This is used for adding a
comment to tell the other programmer about any instruction or guiding him.It is
also used for the programmer to remember its own program. In this particular
case our comment is about the description of our program.
Line 2: #include
<stdio.h>
Lines that start with # are not the
regular programming lines these are the directives for pre-processor to add the
piece of code already made by different programmer which are most often used so
they are already added to the software so that time may be saved and all
programmer don’t have to write that same code every time. A programmer could
also make its own directives.
In our particular case we have added
<stdio.h> which is standard input
output header file. As we have to produce output on screen.
Line 3: void main () {
Every
program must have at least one function that is its main function. No program
could run without a main function you may add multiple function in a
program but the program will start from
its main function. Consider the construction of our main function in this
program void
main () {}
main
is the name of our function followed by parenthesis () because it is the
declaration of function.
In
parenthesis we may define any parameter for our function they are empty because
our program have no parameter. void is the return type of function return type
of any function must be defined it is the expression or value which the
function will return after its execution. We have used (void) which mean empty
our function will not return any value after its execution. The next thing is
{} anything written inside this will be the statement of our main program and
our function consist of these instruction.
It
is also called body of the function.
Line
4: printf("My first program");
This
line is statement. A statement is expression that produces some effect. In our
program this is the only line that produces some effect. printf() is command to
take out put in C lang. inside parenthesis () we write what to output. Anything written within ”” will be shown as it. printf is the function
statement that comes from the <stdio.h> that is why we have included that
file. If we want to take output in any other form other than standard output on
screen then we have to use the different directive or make one of our own.
;
is the most important part of the statement this is the indication of the end
of the statement to the compiler every single statement must be followed by
this semi colon otherwise there will be an error.
Line
5: }
Is
the end of our main function body.
Explanation in C++ for same program.
All
the things remains the same as in C. the changes are as follow.
·
Iostream is the standard input output directive
in C++.
·
Instead of printf(“”); structure
cout << “ ” ; syntax is used.
·
3rd line in C++ that is using
namespace std;
All the elements
of standard C++ library are stored in namespace std so most of source file will
contain this statement in start as we want to use C++ element cout is one of
them used in our program.
Try writing this
program to check your self. Practice make a man perfect. Best of luck. Hope our
blog will help you to reach to the destination of a good programmer.
what is object oriented programming?
Introduction to
object oriented programming.
Basically all the programming languages are of two types
either:
·
Structured Oriented or
·
Object Oriented.
Today most of the languages used are object oriented like
java as2.0, as3.0 and C++.
So the question arises when we say object oriented
programming what we mean by it or what is object oriented programming. This
topic will clear the basic the idea and introduce you to the basic concept of
it.
Object Oriented
Programming.
In object oriented programming we make different objects,
give them some instance names, and then call them by their instance name to
change some of their attributes to perform a function.
Didn’t Understand??
This example will make the idea clear.
Suppose we created a robot as shown below.
We will select this robot and make this an object. Now the
next step is to give it a name that is instance name. We can give any instance
name to it with some restriction which I may explain in any other post. So , suppose
any name for example we gave this robot instance name of robo . Now in our programming if we want the robot to do anything
we can simply call it by its instance name robo.
Our object will have some attributes like its position from
x axis, y axis, its rotation and many other. Suppose we want to move robot
forward when we press right key. We can do this by revealingly increasing its
distance from x-axis.
So our pseudo-code or general algorithmic form for that will be
While (right
key){
robo x-axis increase by 1
}
So in this example we are calling an object and changing its
attributes to do required task this type of programming is called object-oriented
programming.
