0

I'm trying to define a function prem_dern that returns the first index and last index of an input real number ( by the user ). When executing, the intitializing part works fine but not the prem_dern function ( program exits right after compiling. The program doesn't return any errors when compiling.Here is what I did :

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

void prem_dern(float t[], int n, float r)
{   
    int i,j;
    printf("donner le reel pour savoir les positions ");
    scanf("%f",&r);
    for(i=0;i<n;i++)
    {
        if (t[i]==r)printf("la premiere position %d:",i);

        break;
    }
    for (j=n-1;j>0;j--)
    {
        if(t[j]==r) printf("la derniere position %d:",j);
        break;
    }
}
int main()
{   int i,n;
    float r;
    float t[100];
    printf("donner le nombre des elements : \n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("donner un element :");
        scanf("%f",&t[i]);
    }
    prem_dern(t,n,r);
    return 0;
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • 3
    Please describe the problem better than "doesn't work". Give the exact input, expected result and actual result. One problem is that the `break` statements are not actually part of the `if` blocks as you probably intend them to be. – kaylum Nov 02 '20 at 22:52
  • @Hijaw Your function returns nothing. – Vlad from Moscow Nov 02 '20 at 22:58
  • 1
    get rid of trailing semicolons on "if" statements – OldProgrammer Nov 02 '20 at 22:58
  • Hint: If you `break` on the first cycle of a loop every time it's not a loop. – tadman Nov 02 '20 at 23:00
  • 1
    When using `if` you should use `{ ... }` regardless of how many lines are involved. It makes it *very* clear what is and isn't part of the `if` branch. In this case the `break` appears to be part of it, visually, but is in fact not. – tadman Nov 02 '20 at 23:00
  • Tip: Declare variables like `i` and `j` inside the loop itself. Pre-declaring them just clutters up your code, putting them in the loop makes it abundantly clear where the variables originate, and more importantly, what their scope is. `for (int i = 0; ...)` As a bonus you don't need to use two names you can just use `i` unless an inner loop is involved. – tadman Nov 02 '20 at 23:01
  • "not the prem_dern function". Your update doesn't really make it any clearer. All you have really said is "prem_dern doesn't work". Repeat: What is the exact input, expected result and actual result? – kaylum Nov 02 '20 at 23:03
  • Your `if` statements should use braces to group the `printf` and `break` statements together, i.e. `if (t[i]==r) {printf("la premiere position %d:",i); break; }`. This would be easier to see if you had put the `printf` on the line following the `if` and indented the code normally. – Bob Jarvis - Слава Україні Nov 02 '20 at 23:10

1 Answers1

0

A couple of observations that may be contributing to the problem:

This function prototype:

void prem_dern(float t[], int n, float r)

includes no way to return results to the caller. i.e. it is void, and none of the arguments include the address of something to be changed. This is legal, but would be more useful with the ability to do work, then pass its results.

Additionally, the size of the array float t[] is not passed, so the function has no way to know how many elements it contains.

It seems that the declaration of float t[100]; should be moved and changed from:

float r;
float t[100];

To:

scanf("%d",&n);
float t[n];//creating a variable length array that ties array size to n
for(i=0;i<n;i++)
{
     printf("donner un element :");
     scanf("%f",&t[i]);
}

This then provides an explicit tie between the array, and its size:

void prem_dern(float t[], int n, float r)
                          ^^^^^

Next, for execution flow to see the break statement, change the following:

 if (t[i]==r) printf("la premiere position %d:",i);
        break;   

                                  

To:

if (t[i]==r) 
 {
      printf("la premiere position %d:",i)
      break;
 }

Edit to address OP main question

To create ability to return first and last indices of number entered by user modify the struct to return either a pointer to array, or a pointer to struct, or via a function parameter. Here is an adaptation of your function to return via function a struct * parameter:

Given for example this struct definition:

typedef struct {
    int first;
    int last;
    float r;
}index_s;

Here is the adaptation of your function, that will return the float value entered (in the function), and the index of the array that contains same value.

void prem_dern(float t[], int n, index_s *val)
{   
    int i,j;
    int *array = malloc(2*sizeof(*array));
    if(array)
    {
        printf("donner le reel pour savoir les positions ");
        scanf("%f",&val->r);
        for(i=0;i<n;i++)
        {
            if (t[i]==val->r)
            {
                printf("la premiere position %d:",i);
                break;
            }
        }
        for (j=n-1;j>0;j--)
        {
            if(t[j]==val->r) 
            {
                printf("la derniere position %d:",j);
                break;
            }
        }
        val->first = i;
        val->last = j;
    }
}

Calling example:

int main()
{   
    index_s val;//create instance of struct typedef
    //all your other relevant code
    ...
    scanf("%d",&n);
    float t[n];//creating a variable length array that ties array size to n
    for(i=0;i<n;i++)
    {
         printf("donner un element :");
         scanf("%f",&t[i]);
    }
    ...
    prem_dern(t, n, &val);//pass struct as pointer (&)
    val.first; //contains first index
    val.last;  //contains last index
    val.r;     //contains float val searched for
    return 0;
}    
     
ryyker
  • 22,849
  • 3
  • 43
  • 87