I want the following program to use lesser memory as much as possible. Any suggestions?

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

void InputNum(void);
void SearchNum(int);

int numArray[10];


main()
{
int num;


InputNum();

cout<<"\nEnter the number to be searched:";
cin>>num;

SearchNum(num);

getch();
}


void InputNum(void)
{
for(short i=0;i<=9;i++)
{
cout<<"\nNumber "<<(i+1)<<":";
cin>>numArray[i];
}
}


void SearchNum(int n)
{
bool found = 0 ;
for(short i=0;i<=9;i++)
{
if((n == numArray[i]))
{
found=1;
break;
}
}

if (found)
{
cout<<"NUMBER FOUND!";
}
else
{
cout<<"NUMBER NOT FOUND!";
}
}

Recommended Answers

All 2 Replies

First tell us how you are measuring its memory usage, and the basis on which you think that usage needs to be reduced.

It would help if you used a recent compiler/library and used standard functions/etc. There is no <iostream.h> (except with old compilers) and <conio.h> is system-specific.

Memory usage of a small program like you've shown is really determined by characteristics of the compiler and its library - as are techniques to reduce memory usage.

Your going into searching and sorting algoithms. If the numbers are in ascending order then you can perform a bnary search which optimizes speed for finding the number. If is is not sorted into order then you must perform a sor of some kinf such as a quick sort. check these out on wikipedia

i can provide more detail if needed

Nevermind i read the question wrong

Chris

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.