For example i have an array like:
a[20]={1,5,7,8,4,3,12,15,15,15,11,...}
lets say that the max Value of this array is 15.

i want to write :
"The max value of array a is 15 and it is located at 8th,9th and 10th element in array. "

Is it possible to write a C code to solve this? Thanks..

Recommended Answers

All 6 Replies

Yes

How then my friend?

Sorry I thought that was a very accurate answer to the actual question you posted. Perhaps you should try doing your own homework and then asking more explicit questions if you run into trouble.

I for one am not going to be writing your homework assignment for you and I doubt anyone else here will either.

Here are a few hints

  • You are going to need to check every element in the array so some sort of loop will probably be required

  • You are looking for the largest value so you will need to be doing some comparisons, that means if statements and compaison operators
  • You are looking for more than 1 result so you will possibly require an array to store the results in.
  • To make things simple start by writing a program that just finds the largest value in an array, then once it is working modify it to actually work out the indexes in the array that the value is stored at.

If you only have 10 hours to complete it you better get cracking!

Next time please don't PM me.

I'm close to the end.

#include <stdio.h>
#include <math.h>
#define MAX 100
int main(int argc, char *argv[])
{
  int N=0 , i=0, j;        /*counters*/
  float circle[MAX] [3] ;  /*To define a circle as x y r*/
  float result1, result2, result3;  /*To simply math functions*/
  int maxIndex=0,index=0,mxm=0; /*For finding max value of an array*/
  int intersection[MAX], containment[MAX]; /*For creating arrays from the results of the equations*/
  
  printf("Please enter the number of circles you want to compare : \n");
  scanf ("%d", &N);  /*Number of circles that are gonna be compared*/
  
  for(i=0; i<N; i++){
  printf ("Enter the values of x, y and r for circle %d: \n", i+1);
  scanf ("%f %f %f", &circle[i] [0],&circle[i] [1],&circle[i] [2] );
  } /*x y and r values for circle 1 to N*/

    for(i=0; i<N; i++){
    intersection[i]=0;
    containment[i]=0;
    }/*Initializing arrays*/

     /*Intersection Situation*/
     for(i=0; i<N; i++){
        for (j=i+1; j<N; j++){
         result1 =sqrt(((circle[i][0]-circle[j][0])*(circle[i][0]-circle[j][0]))+ ((circle[i][1]-circle[j][1])*(circle[i][1]-circle[j][1])));
         result2 =(circle[i][2]+circle[j][2]);
              /*((circle[i][0]!= circle[j][0])||(circle[i][1]!= circle[j][1])||(circle[i][2]!= circle[j][2])) is used to rule out identical circles.*/
              if ((result1 <= result2) && (result1 > 0.0)&& ((circle[i][0]!= circle[j][0])||(circle[i][1]!= circle[j][1])||(circle[i][2]!= circle[j][2]))) {
              intersection[i] = intersection[i] + 1;
              intersection[j] = intersection[j] + 1;
              }
        }
     }

     /*Containment Situation*/
     for(i=0; i<N; i++){
        for (j=0; j<N; j++){
         result1 =sqrt(((circle[i][0]-circle[j][0])*(circle[i][0]-circle[j][0]))+ ((circle[i][1]-circle[j][1])*(circle[i][1]-circle[j][1])));
         result3 =(circle[i][2])-(circle[j][2]);
              /*((circle[i][0]!= circle[j][0])||(circle[i][1]!= circle[j][1])||(circle[i][2]!= circle[j][2])) is used to rule out identical circles.*/
              if((result1 <= result3) && (i!=j)&& ((circle[i][0]!= circle[j][0])||(circle[i][1]!= circle[j][1]||(circle[i][2]!= circle[j][2])))){
              containment[i]=containment[i] + 1;}
        }
     }

               /*Finding Max Value of Intersection Array  */
               mxm=intersection[0];
               for(index=1;index<N;index++){

               if(intersection[index]>mxm){
               mxm=intersection[index];
               maxIndex=index;}/*The element of array thats value is maximum*/
               }
     if(mxm==0){ /*If maximum value of the array is zero*/
     printf("There is no intersection. \n");
     }

     else {
     printf("Circle with most intersections is circle #%d (x y r):(%.2f %.2f %.2f)\n", maxIndex+1,circle[maxIndex][0],circle[maxIndex][1],circle[maxIndex][2]);
     printf("Count of intersections: %d \n",mxm);
     }


              /*Finding Max Value of Containment Array  */
              mxm=containment[0];
              for(index=1;index<N;index++){

              if(containment[index]>mxm){
              mxm=containment[index];
              maxIndex=index;}/*The element of array thats value is maximum*/
              }

     if(mxm==0){/*If maximum value of the array is zero*/
     printf("There is no containment. \n");
     }

     else {
     printf("Circle with most containments is circle #%d (x y r):(%.2f %.2f %.2f)\n", maxIndex+1,circle[maxIndex][0],circle[maxIndex][1],circle[maxIndex][2]);
     printf("Count of containments: %d \n",mxm);
     }


  system("Pause");
  return 0;
}

My homework is like this now. A few little problems though. I can not rule out identical circles from containment array. Its getting a bit hard when you consider concentric circles. I'm asking for one last help of yours. Any idea how to correct this situation?

any idea? i have 1.5 hours left to deliver ..

There is a slight problem here, you have used floats, which I wouldn't say is wrong, but the problem is that floats (and doubles) are approximations to values. What this means is that 2 variables that you and I would say where the same the computer will think is different because it differs in the 7th significant digit.

You can't use == and != on floats and doubles so you can't say that 2 circles definately have the same centre or definately have the same radius.

But if they have the same centre they are concentric and if they have the same radius as well they are the same circle.

You have to write a test for float similarity that has a tolerance, some thing like fabs(float1 - float2) < 0.001, is the absolute difference less than some tolerance.

You can then use this test on the centre of you circles to and you will be able to say the circles are close to concentric and if they also have the same radius they will be close to being the same circle.

You should do these tests first because you do not want to be trying to find the intersections of 2 circles that may be close to being the same circle, there will be a lot of them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.