Pages

Showing posts with label Dynamically allocating memory. Show all posts
Showing posts with label Dynamically allocating memory. Show all posts

Saturday, June 29, 2013

FINDING MEDIAN AND AVERAGE IN C++

/* QUESTION :

Write a program to store integer values to a table (length of table during program execution)

using double pointer. Pass the table as arguments to a function. Print Median and average

values.

Hint: Your will use new operator to allocate memory.

Note: You have to release the memory using delete operator before the end of program. */



// CODE :


 #include<iostream>
using namespace std;

void find_median_average(double **ptr,int a,int b);

//MAIN STARTS
int main(){
 int a,b;
 cout<<"Enter the no of rows";
 cin>>a;
 double **ptr = new double *[a];

 cout<<"Enter the no of coloumns";
 cin>>b;
 for (int i=0;i<a;i++){
  ptr[i] = new double[b];
 }
 cout<<"\n\nEnter the values\n";
 for (int j=0;j<a;j++){
  for(int k=0;k<b;k++){
   cin>>ptr[j][k];
  }
 }

 find_median_average(ptr,a,b);


 for(int p=0; p< a;p++)
  delete [] ptr[p];
 delete [] ptr;
        ptr = NULL;
 return 0;
}


//END MAIN

void find_median_average(double **ptr,int a,int b){
 double average = 0;

 for (int l=0;l<a;l++){
  for(int m=0;m<b;m++){
   average += ptr[l][m];

  }
 }
 average /= (a*b);
 cout << "Average: " << average << endl;
 double median = 0;

 int positionTerm1=(a*b)/2;
 int positionTerm2=(a*b)/2+1;
 

 int count1 = 1;//How many times the loop has Run.
 int count2 = 1;
 int js = 0;
 bool breakFromInnerMostLoop = false;
 for (int i = 0; i<a; i++){
  for (int j = 0; j<b; j++){
   js = j;
   if (count1==positionTerm1){
    breakFromInnerMostLoop = true;
    break;
   }
   count1++;
  }
  if (breakFromInnerMostLoop){
    break;
  }
 }
 breakFromInnerMostLoop = false;//reset

 cout << "i: " << i << endl;
 cout << "js: " << js << endl;


 int ys = 0;

 for (int x = 0; x<a; x++){
  for (int y=0; y<b; y++){
   ys = y;
   if (count2==positionTerm2){
    breakFromInnerMostLoop = true;
    break;
   }
   count2++;
  }
  if (breakFromInnerMostLoop){
    break;
  }
 }


 cout << "positionTerm1: " << positionTerm1 << "\npositionTerm2:" << positionTerm2 << endl;
 cout << "x: " << x << endl;
 cout << "ys: " << ys << endl;
 if ((a*b)%2==0)
  cout << "Median: " << (ptr[i][js] + ptr[x][ys])/2 << endl;
 else
  cout << "Median: " <<   (ptr[x][ys]) << endl;
}

 

Thursday, June 27, 2013

C++ CODE FOR DYNAMIC MEMORY ALLOCATION (DMA)

QUESTION:
Write a program to allocate memory locations for an array of ‘int’ type using ‘new‘ operator.

The program should input values for the length of array during program execution. Enter values

into array and find maximum values of an array.

Note: You have to release the memory using delete operator before the end of program.
 
 


CODE:

#include <iostream>
using namespace std;
int main(){
int size;
cout<<"Enter the size of the array\n";
cin>>size;
int *ptr = new int[size];
cout<<"\nEnter the values in the array\n\n";

for(int i=0;i<size;i++){
cin>>ptr[i];
}
int max = ptr[0];
for(i=0;i<size;i++){
 if(ptr[i] > max){
    max = ptr[i];
 }

}
cout<<"Maximum value of array is : "<<max<<endl<<endl;
delete [] ptr;
ptr = NULL;

 return 0;
}

 

Wednesday, June 26, 2013

Structure c++ implementation code

Phone Number Example


A phone number can be thought of three parts: the area code (051), the exchange (767),
and the number (8977). Write a program that uses a structure to store these three parts of
phone number separately. You have to ask user how many phone numbers he/she wants
to store. Save these phone numbers dynamically.
Then Program should ask user to enter the area code.
Output should be the phone numbers that matches with the given area code in the
following format.

(area code) exchange-number
(051) 767-8977


CODE:
#include <iostream>
using namespace std;

struct p_no{
   long int area_c;
   long int exchange;
   long int no;

};


int main(){
cout<<"How many phone number's you want to enter : ";
int ph_no;
cin>>ph_no;


p_no *ptr = new p_no[ph_no];

for (int i=0; i<ph_no ; i++){
cout<<"\nENTER AREA CODE\n";
cin>>ptr[i].area_c;

cout<<"\nENTER EXCHANGE NUMBER\n";
cin>>ptr[i].exchange;

cout<<"\nENTER THE NUMBER\n";
    cin>>ptr[i].no;
}


for (int j=0; j<ph_no ; j++){
cout<<"\nEnter the area code\n";
long int area_code;
cin>>area_code;
if (area_code == ptr[j].area_c)
cout<<"\n\n("<<ptr[j].area_c<<")"<<ptr[j].exchange<<"-"<<ptr[j].no<<endl<<endl;



}

return 0;
}