Write a C++ program called min_distance.cpp that reads ten integer numbers from
ID: 3623146 • Letter: W
Question
Write a C++ program called min_distance.cpp that reads ten integer numbers from a user and displays the closest distance between two numbers in the numbers. For example, if your program received numbers such as -1, 10, -15, 3, 7, 20, 150, 200, 50, and 4, the answer will be 1 because the distance between 3 and 4 is 1. Note that the distance between two numbers is always positive. The following presents a sample execution of the program:Enter ten numbers: -1 10 -15 3 7 20 150 200 50 4
Minimum distance is 1 between 3 and 4.
Explanation / Answer
please rate - thanks
#include <iostream>
#include<cmath>
using namespace std;
int main()
{int mat[10][10]={0}, num[10],i,j,min;
int mini,minj;
cout<<"Enter 10 numbers:";
for(i=0;i<10;i++)
cin>>num[i];
for(i=0;i<10;i++)
for(j=0;j<10;j++)
mat[i][j]=abs(num[i]-num[j]);
min=9999999; //big number
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(mat[i][j]>0)
if(mat[i][j]<min)
{mini=i;
minj=j;
min=mat[i][j];
}
cout<<"minimum distance is "<<mat[mini][minj]<<" between "<<num[mini]<<" and "<<num[minj]<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.