Answer to Question #271440 in C for Shinu

Question #271440

you have been an integer array A of size N.you need to pfint the number with the value closest to zero.if there are multiple elements print the number with greater value.

1
Expert's answer
2021-11-25T18:04:44-0500
#include <iostream>
using namespace std;
int main()
{
    int num;
    printf("Enter the size of the array: ");
    scanf("%d",&num);
    int *A = new int[num];
    for(int i =0;i<num;i++)
    {
      printf("Enter %d" ,i);
      printf(" Element of the array: ");
      scanf("%d",&A[i]);
    }
    int E_index=0;
    for(int i =0;i<num;i++)
    {
      if ((A[i] >= 0 && A[i]<=A[E_index]) || (A[i]< 0 && A[i]>A[E_index]))
        {
        E_index = i;
        }
    }
    printf("element closest to zero A[%d",E_index);
    printf("]=%d",A[E_index]);
    delete[] A;
    A= nullptr;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS