So i've redone most of the code and made a few tests.But my current problem is that the code in the function largestElements that finds the largest of every 5 elements works without problems when its not inside of a function.It just crashes when i choose option 2.I dont know what causes these crashes.If someone can tell me things i should check or correct would be great
Code:
#include <visual_2013.h>
#include <stdlib.h>
#include <stdio.h>
#define ARRAY_SIZE 10
#define MAX_SIZE (ARRAY_SIZE / 5)
#define REST_SIZE (ARRAY_SIZE - MAX_SIZE)
void enterArray(a);
void largestElements(int array[], int max[], int rest[]);
int main()
{
int array[ARRAY_SIZE];
int rest[REST_SIZE];
int max[MAX_SIZE];
int option = 0, i = 0, n = 0, surplus = 0, a;
char *menu[] = { "MENU", /*menu initializa*/
"1.Enter new array",
"2.Edit existing array",
"3.End" };
do {
system("cls");
for (i = 0; i < 4; i++) /*switch options*/
printf("\n%s\n", menu[i]);
printf("\n\nChoose an option from [1-3]: ");
scanf("%d", &option);
switch (option)
{
case 1:
system("cls");
printf("You chose to enter a new array!\n");
for (i = 0; i < ARRAY_SIZE; i++)
{
printf("\nElement(%d) will be equal to : ", i + 1); /*Entering values for the array*/
scanf("%d", &array[i]);
}
printf("You successfully entered a new array.\n");
printf("\nThe current array has values of :\n"); /*Prints the values of the array*/
for (i = 0; i < 10; i++)
{
printf("%d,", array[i]);
}
system("pause");
break;
case 2:
largestElements(array[ARRAY_SIZE], max[MAX_SIZE], rest[REST_SIZE]);
break;
case 3:
break;
default:
printf("Enter a valid option");
system("pause");
}
} while (option != 3);
system("pause");
return 0;
}
void enterArray(int array[]) /*Function for entering values for the array*/
{
int i;
printf("Vuvedi elementite na masiva ((100) elementa): \n");
for (i = 0; i < ARRAY_SIZE; i++)
{
printf("a[%d]=", i);
scanf("%d", &array[i]);
printf("\n");
}
}
void largestElements(int array[], int max[], int rest[]) /*Function which determines the largest element of every 5*/
{
int i, surplus, n;
for (i = 0; i < MAX_SIZE; i++)
{
max[i] = array[i * 5];
for (n = ((i * 5) + 1); n < ((i * 5) + 5); n++)
{
if (array[n] > max[i])
{
rest[surplus] = max[i]; surplus++;
max[i] = array[n];
}
else
{
rest[surplus] = array[n]; surplus++;
}
}
}
printf("main array :\n");
for (i = 0; i < ARRAY_SIZE; i++)
{
printf("Element number %d = %d\n", i + 1, array[i]);
}
printf("max array :\n");
for (i = 0; i < MAX_SIZE; i++)
{
printf("MAX number %d = %d\n", i + 1, max[i]);
}
printf("Rest :\n");
for (i = 0; i < REST_SIZE; i++)
{
printf("surplus %d = %d\n", i + 1, rest[i]);
}
}