-3
#include "stdafx.h"
using namespace std;
#include <iostream>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;

//Search program
 void read_data(int* , int); //array address, size
int seq_search(int , int* , int);//key,array address,size
int binary_search(int , int* , int);

void main(void)
{
int  *arr , size , ans , key ;
cout << "Please enter the size: ";
cin >> size ;
arr = new int[size] ;
read_data(arr , size);
cout << "Enter the number to search for: ";
cin >> key ;
ans = binary_search(key, arr , size);
if(ans == -1)   
    cout << "The number " << key << " does not exist \n";
else    
    cout << "The number " << key << " exists at location " << ans << endl;
getch();
}

void read_data(int *pt , int ss)
{
cout << "Please enter " << ss << " numbers: \n";
for(int k = 0 ; k < ss ; k++)
{
    cout << "Enter the " << k + 1 << " number: " ;
    cin >> pt[k] ;
}
}

int seq_search(int num , int a[] , int s)
{
for(int k = 0 ; k < s ; k++)
    if(num == a[k])     { return k ; }
return -1 ;
}

int binary_search(int num , int a[] , int s)
{
int first , last , mid ;
first = 0 ;     last = s -1 ;
while(first <= last)
{
    mid = (first + last) / 2 ;
    if(num == a[mid])               return mid ;
    else if(num > a[mid])       first = mid + 1 ;
    else                                    
last   = mid - 1 ;
}
return -1;
}

From what I understood (I'm a real beginner) is for example *P = a; points to address if integer a, and p = %a; is the reference or the real address of a.

I understood that we use this new int in order to use the array everywhere in the program as it is destroyed when main is finished, but why didn't I declare it outside of main too to be used everywhere?

trincot
  • 317,000
  • 35
  • 244
  • 286
Wall-ED
  • 51
  • 5
  • 1
    This code is extremely hard to read... please look into proper indenting and code formatting! – TypeIA Mar 21 '14 at 16:30
  • If you are in an IDE, there should be a formatter. But you should know the general structure for a language. – TIMBERings Mar 21 '14 at 16:31

2 Answers2

1

I understood that we use this new int in order to use the array everywhere in the program as it is destroyed when main is finished, but why didn't I declare it outside of main too to be used everywhere?

Whoever told you such a thing hates you with all his/her heart. Trust me.

You generally don't want to use new for nothing. The new operator dynamically allocates memory which as a last resort will be destroyed when the main exists (the programs end), even though I'm not sure this is actually guaranteed. But dynamically allocated memory that is not deleted will cause memory leak, which can be pretty dangerous in a program.

Most of the time there are better way to handle dynamically allocated resources and arrays of dynamic size (in this case std::vector).

Just read a good book on C++ and learn what new and delete actually means, and why you should avoid them.

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • My professor mentioned nothing on memory management which is most of what I find out when I read books c++ he justified it by saying we need to do so because the compiler can't compile a matrix without knowing how many address its going to take and because it will be destroyed out of main and it was left ambiguous – Wall-ED Mar 21 '14 at 17:01
0

To declare it outside main you would have to know the size of the array at compile time

prmottajr
  • 1,816
  • 1
  • 13
  • 22