// Algorithm_LINNET1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <ctime>
#define SIZE 10
using namespace std;
class stack
{
int stck[SIZE];int tos;
public :
stack(); void push(int num);int pop();
};
stack::stack() {tos=0;}
void stack::push(int num){ stck[tos]=num;tos++;}
int stack ::pop()
{
if (tos==0){cout << "stack is empty\n"; return 0;}
tos--;return stck[tos];} void RN(int array[], int n);
void INSERTIONSORT(int array[], int n);
int LINSEARCH(int array[], int n, int key);const int n=10;
int array [n];
int main()
{
srand((unsigned int)time(0));RN(array, n);cout << endl;
INSERTIONSORT(array, n);cout << endl; int key=56;
int result=LINSEARCH(array, n, key); if (result ==-1)
cout << "key is not found"; else cout << "key is found"<<endl;
stack stk;int i, max =array [0]; for (i=1;1<n;i++)
if (array [i]>max)max=array[i]; stk.push(max);
cout << "\n top element is ="<<stk.pop()<<endl; return 0;
}
void RN(int array[], int n) {
for (int i=1;i<=n;i++)
{
array [i-1]=1+rand()%100; cout << setw(5)<<array[i-1];
}
}
int LINSEARCH (int array[], int n, int key)
{
for (int i=0;i<n;i++) {
if (array [i]==key)return i;return (-1);
int j=1;
for (;j<n;j++) {
int key=array [i];int i=j-1;
It is as it says: it is possible for LINSEARCH function to take path where there no return statement and your program behavior is undefined. One I spotted instantly: what if n is 0?
@MiiNiPaa
I truly appreciate your answer,
I just tried n=0 didn't work, same error is being shown.
should I return in my LINSEARCH or other ways ?
would you please help me ?
Yes, you should make sure that you would fulfill your obligation to return int from a function.
Actually, there is a problem in your function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
int LINSEARCH (int array[], int n, int key)
{
for (int i=0; i<n; i++) {
if (array [i]==key)
return i;
return (-1); //It will unconditionally return -1 on first iteration.
//So your code can either return 0 if item is found on first position
//-1 in other cases or nothing if n = 0.
int j=1;
/* Code here is irrelevant as it would be never executed */
}
//You probably want to move return -1 here.
}