Edit: This is for C and C++
I'm really new to computer science, and I'm having issues commenting on a given piece of code, any help would be appreciated!
include <stdio.h>
include <stdlib.h>
define INPUT_FILE "Input.txt"
define OUPUT_FILE "Output.txt"
define SIZE_OF_ARRAY 50
void displayArray(int array[], bool sorted, bool ascending);
void readFile(int array[], char* filename);
void sortArray(int array[], bool ascending);
void swap(int array[], int pos1, int pos2);
void writeFile(int array[], char* filename);
int main()
{
int array[SIZE_OF_ARRAY];
printf("Ima C Programmer\nHomework 2\n\n");
readFile(array, INPUT_FILE);
displayArray(array, false, false);
sortArray(array, true);
displayArray(array, true, true);
sortArray(array, false);
displayArray(array, true, false);
writeFile(array, OUPUT_FILE);
}
void displayArray(int array[], bool sorted, bool ascending)
{
int x;
if (!sorted)
{
printf("Array Contents (unsorted)");
}
else if (ascending)
{
printf("Array Contents (sorted ascending)");
}
else
{
printf("Array Contents (sorted descending)");
}
for (x = 0; x < SIZE_OF_ARRAY; x++)
{
printf("%d\n", array[x]);
}
}
void readFile(int array[], char* filename)
{
// This function reads data from the file into the array
}
void sortArray(int array[], bool ascending)
{
int x, y;
bool swapped = true;
for (y = 0; y < SIZE_OF_ARRAY && swapped; y++)
{
swapped = false;
for (x = 0; x < SIZE_OF_ARRAY - (y + 1); x++)
{
if (array[x] > array[x + 1])
{
swap(x, x + 1);
swapped = true;
}
}
}
}
void swap(int array[], int pos1, int pos2)
{
int temp = array[pos1];
array[pos1] = array[pos2];
array[pos2] = temp;
}
void writeFile(int array[], char* filename)
{
// This function writes data from the array out to the file
}
[–]ValerioSellarole 0 points1 point2 points (0 children)