Click to See Complete Forum and Search --> : beginners trouble!


sunshine1
June 5th, 2005, 11:02 PM
can anyone xplain this code to me. i have just started to learn C++. i don't understand how we can pass a pointer as an argument in the function call. i dont understand this. plz help.

#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>
void small(int *, int &);
int main()
{
int *ptr,n,k,min;
cout<<"enter n";
cin>>n;
ptr=new int[n];
for(int i=0;i<n;++i)
cin>>ptr[i];
small(ptr,n);
delete []ptr;
system("PAUSE");
return 0;
}

void small(int *arr,int &k)
{
int min=arr[0],pos=0;
for(int i=0;i<k;i++)
{
for(int j=i+1;j<k;j++)
{
if(arr[j]<min)
{
min=arr[j];
pos=j;}
}
}
cout<<"the minimum num is"<<min;
cout<<endl<<"its position is"<<pos;
}

Smasher/Devourer
June 5th, 2005, 11:33 PM
can anyone xplain this code to me. i have just started to learn C++. i don't understand how we can pass a pointer as an argument in the function call. i dont understand this. plz help.
I don't understand what you're asking exactly. Why would you not be able to pass a pointer as an argument? Please be a little more specific as to what, precisely, you're having trouble with.

I will make a few comments on this program, though. The most obvious thing I noticed was that the function small() is needlessly complicated. Since it just walks through the array once, looking for the smallest entry, only one for loop is needed. The nested loop structure contained in your example looks like it was intended for use in an exchange sort or something similar. A somewhat simpler approach might look like this:
void small(int *arr, int &k)
{
int min = arr[0], pos = 0;

// We can start at 1 here instead of 0; no need to compare element
// 0 against itself
for (int i = 1; i < k; i++)
{
// only one loop necessary -- check every element once
if (arr[i] < min)
{
min = arr[i];
pos = i;
}
}

cout << "the minimum num is" << min;
cout << endl << "its position is" << pos;
}
Is there any reason in particular that you're passing k as a reference, instead of simply passing it by value? You're not modifying k in the function so that's nothing to worry about; and it's just a basic type, not a large object where passing by value would be inefficient, so space isn't a concern. Just curious...

One other thing -- about your headers. You have:
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
The last three are not necessary; you're not using anything from them. Only the first is necessary, and even then, iostream.h is not the standard C++ header. Use <iostream> instead. The objects defined therein are part of the std namespace, so you either need to reference that specifically when using objects from iostream, for example:
std::cout << "the minimum num is " << min;
or put a using line at the top of your program, probably right beneath your #includes:
#include <iostream>
using namespace std;
Similarly, use the C++ standard headers cstdio and cstring rather than stdio.h and string.h when you need them. (I'm not entirely sure about conio.h, but I haven't seen that one since I used to use Borland Turbo C++ in DOS. Is it a Borland-specific header?)

Finally, the variables k and min are not used in main(), so you don't need to declare them. You use k and min in the small() function, and so they're defined there, and that's enough. Change this line:
int *ptr, n, k, min;
to:
int *ptr, n;