c++ help urgently needed

Write a C++ program to find the second smallest number and its position in an integer array. (Note, the length of an array and elements in the array should be determined by using cin).


what i did is the following but i think i'm wrong and it only gives the second smallest number but not the position. and i didn't use cin. how do i find it. here is what i got.

#include <iostream>
int main(){
int a[50],size,i,j=0,small,secondsmall;
printf("Enter the size of the array: ");
scanf("%d",&size);
printf("Enter %d elements in to the array: ", size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);

small=a[0];
for(i=1;i<size;i++){
if(small>a[i]){
small=a[i];
j = i;
}
}

secondsmall=a[size-j-1];
for(i=1;i<size;i++){
if(secondsmall > a[i] && j != i)
secondsmall =a[i];
}

printf("Second smallest: %d", secondsmall);
system("pause");
return 0;
}
Hi,
Below sample might help!
#include<stdio.h>
#include<conio.h>

void main()
{
int a[] = {21,39,5,15,12};
int max,min,smin,sminpos;

clrscr();

min = max = smin = a[0];

for(int i=0;i<5;i++)
{
if(a[i]<min)
{
min = a[i];
}
if(a[i]>max)
{
max=a[i];
sminpos = i;
}

}

smin = max;

for( i=0;i<5;i++)
if(a[i] != min)
if(a[i]<smin)
{
smin = a[i];
sminpos = i;
}

printf("min = %d and smin = %d,sminpos = %d",min,smin,sminpos+1);
}
i put it on c++ and got 2 errors. one is
error C3861 clrscr()
error C2065 undeclared identifier
and after debugging it only gives the second smallest number which i got already in my code but i wanna show the position too. that's why i asked the question.

what's the solution for that
Last edited on
i actually got a solution for the error C3861 clrscr()
here it is...but what's error C2065 undeclared identifier and how can i show the position of the number in the provided list

#include <iostream>
#include <stdlib.h>
int main()
{
int a[] = {21,39,5,15,12};
int max,min,smin,sminpos;

system("cls");

min = max = smin = a[0];

for(int i=0;i<5;i++)
{
if(a[i]<min)
{
min = a[i];
}
if(a[i]>max)
{
max=a[i];
sminpos = i;
}

}

smin = max;

for( i=0;i<5;i++)
if(a[i] != min)
if(a[i]<smin)
{
smin = a[i];
sminpos = i;
}

printf("min = %d and smin = %d,sminpos = %d",min,smin,sminpos+1);
}
it's best to avoid calling system() because (a) it's a security risk and (b) commands vary from os to os
try
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;

int main()
{
    int numElements;
    vector<int>elements;
    int min=0x7fffffff,min2=0x7fffffff,min2pos=0,nValue;
    cout << "Enter the number of elements in your array:\n";
    cin >> numElements;
    cout  << "Enter the numbers in the array:\n";
    for (int i=0; i<numElements; i++)
    {
        cin >> nValue;
        elements.push_back(nValue);
    }
    for (int i=0; i<elements.size(); i++)
    {
        if (elements[i]<min) min=elements[i];
        else if (elements[i]<min2)
        {
            min2=elements[i];
            min2pos=i;
        }
    }
    cout << "The second-smallest value was " << min2 << " and it was found at element " << min2pos << endl;
    system("pause");
}


I know i'm going against what I just said with my system() call.
Last edited on
If you run this program from the command line, you can avoid using system("pause")
Hello richcohen,can you give a try for below code:

#include<stdio.h>

void main()
{
int a[] = {21,39,5,15,12};
int max,min,secmin,secminpos,i;

min = max = secmin = a[0];

for(i=0;i<5;i++)
{
if(a[i]<min)
{
min = a[i];
}
if(a[i]>max)
{
max=a[i];
secminpos = i;
}

}

secmin = max;

for( i=0;i<5;i++)
if(a[i] != min)
if(a[i]<secmin)
{
secmin = a[i];
secminpos = i;
}

printf("smallest = %d , second smallest value = %d and it's position = %d",min,secmin,secminpos+1);

return 0;
}
Topic archived. No new replies allowed.