#Function Call

14 messages · Page 1 of 1 (latest)

icy sundialBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

#
// 4. Call function_1( ), which will process the contents of the
//    inventory array by sorting the inventory array to place all the cars
//     in ascending order according to their price.
function_1(int inventory[]);

// 5. Call function_2( ), which will save (write) the sorted inventory
//    array to a file named output.dat

// 6. Terminate
return 0;
}

// Provide the definition for your PCFs below:

int function_1(int array[]) {
  int i, j, year = 0, n = 25;

  for (i = 0; i < n; ++i) {
    for (j = i + 1; j < n; ++j) {
      if (array[i] > array[j]) {
        year = array[i];
        array[i] = array[j];
        array[j] = year;
      }
    }
  }

  printf("The price in ascending order is:\n");
  for (i = 0; i < n; ++i) {
    printf("%d\n", array[i]);
  }

  return array[i];
}
Fady
upbeat mason
#

What is your question?

crimson sinew
#

How i would create a function to sort from price from an array, then call that function

uncut goblet
#

What have you tried?

crimson sinew
#

Function_1 sorts the array in ascending order (from a .dat file) and prints the contents, however, when ran, nothing is printed

#

Full code listed above

upbeat mason
#

!code

icy sundialBOT
crimson sinew
#

*/

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

typedef struct cars {
char make [ 20 ] ;
char model [ 20 ] ;
char color [ 15 ] ;
int year ;
double price ;
} cars ;

//Provide the prototype for your PCFs below:

int function_1( int inventory[]);

int main (void)
{
//Local variables declared... add additional variables as required
cars inventory [ 25 ] ;
int i = 0;

 //1. Open the cars.dat file in the read mode and confirm
//     you have properly opened the file.
    FILE  * read_ptr = NULL;
    read_ptr = fopen("cars.dat", "r");

    if("cars.dat"!= 0){

            printf("\nThe file was opened correctly\n");
    }
    else{

            printf("\nThe file was not opened correctly\n");
    }

 //2. Read the entire contents of the cars.dat file into the
 //    inventory array.
    for(i=0; i<25; ++i){

            fscanf(read_ptr, "%d", &inventory);
    }

 //3. Close the cars.dat file
    fclose(read_ptr);

 //4. Call function_1( ), which will process the contents of the
 //    inventory array by sorting the inventory array to place all the cars
 //     in ascending order according to their price.
    function_1(int inventory[]);

//5. Call function_2( ), which will save (write) the sorted inventory
//    array to a file named output.dat

//6. Terminate
return 0 ;

}

//Provide the definition for your PCFs below:

#

int function_1(int array[])
{

    int i, j, year = 0, n = 25;

    for (i = 0; i < n; ++i){
        for (j = i + 1; j < n; ++j){
            if (array[i] > array[j]){
                year = array[i];
                array[i] = array[j];
                 array[j] = year;
             }
        }
    }


    printf("The price in ascending order is:\n");
            for (i = 0; i < n; ++i){
               printf("%d\n", array[i]);
            }

    return array[i];

}

icy sundialBOT
#

```cpp
int main() {
return 0;
}
```

icy sundialBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.

icy sundialBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.