-1

my programm should calculate many values of a variable that changes in a while loop, store theses values in an Array and then search the maximum value.

the problem i faced is that the values are not stored, when the loop is incremented, the new calculted value crush the precedent one and then how can i compare them?

I just know scanf to store but it doesn't work in this case, should i use another pointer to go over the array values???

//// Simple Program to explain the problem!!!

#include <stdio.h>
#include <stdlib.h>
#include<math.h>

float *tab;
int main()
{
float a;
a=-1;
int i;
float tab[maxi]=0;
int taille=10;

for (i=0;i<taille; i++)
{
   while (a < 30)
     {
   float s= -a*a+a+1;
   tab=(float(*)) malloc(taille*sizeof (float));
   tab[i]=s;

   if(tab[i]>= tab[maxi])
   {
       maxi=tab[i];
       printf("\n maxi=%f",maxi);
       break;
   }
   a=a+1;
     }

 }

  return 0;
}

thanks in advance!

zineb
  • 9

3 Answers3

0

This doesn't work because you allocate the array again and again in the loop, so every allocation overwrites (replaces) the previous allocation. The statement 'float tab[maxi]= 0;' writes to unallocated memory (you got a segmentation fault or alike?)

The logic of what you want to do is not clear, but the following is at least correct C:

float *tab;
int main()
{
    float a= -1.0;
    int i;
    int taille=10;
    int maxi= taille-1;

    tab=(float *) malloc(taille*sizeof(float));
    tab[maxi]= 0;

    for (i=0;i<taille; i++)
    {
        while (a < 30)
        {
            float s= -a*a+a+1;
            tab[i]=s;

            if(tab[i]>= tab[maxi])
            {
                maxi=tab[i];
                printf("\n maxi=%f",maxi);
                break;
            }
            a=a+1;
        }
    }
    return 0;
}

P.s.: the loop while (a<30) indeed overwrites tab[i] everytime. I assume this is what youu want, ot ir may be the error (one of them) you are looking for.

Paul Ogilvie
  • 25,048
  • 4
  • 23
  • 41
0

As i assume, when looking at your printf, you want to store the maximum number in maxi? tab[maxi] gives you the (maxi+1)th number in the Array, not the maximum one.

At the moment you compare all numbers, if they are bigger as the maxi+1th number in the array. You said, that you wanted to store all numbers and then search for the biggest one.

In this case you could use a variable

float maxi=0;

Compare this variable to every new number. If the number is bigger, you override it, if not you do nothing.

if(tab[i]>= maxi) maxi=tab[i];

Your printf comes after the loop, when your done comparing all numbers. At the moment your searching/printing the maximum number, before you even know all numbers.

Kai
  • 103
  • 4
0

Thanks everyone! Actually it was simple as Kai suggests: using a variable maxi here is the programm, it works:

#include <stdio.h>
#include <stdlib.h>
#include<math.h>

float *tab;
int main()
{
float a= -1;
int i;
int taille=10;

tab=(float *) malloc(taille*sizeof(float));
float maxi=0;

for (i=0;i<taille; i++)
{
    while (a < 10)
    {
        float s= -a*a+a+1;
        tab[i]=s;

        if(tab[i]>= maxi)
        {
            maxi=tab[i];
            break;
        }
        a=a+1;
    }

}
printf("\n maxi=%f",maxi);
return 0;

}

zineb
  • 9