0

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!";
}
}
3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Freaky_Chris
0

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.