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]);
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.
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;
#include <iostream>
#include <vector>
#include <cstdlib>
usingnamespace 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];
elseif (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.