i have trouble for using these array function to be able to use it on MQL4 strict mode.
Could anyone hint me where i should start? The purpose is so it could work on strict mode.
int Trigger;
void function6(int &arrays[50])
{
int vals = ArraySize(arrays);
int trailingstop = TimeCurrent() - (arrays[function5(arrays) - 1]);
while(trailingstop > Trigger)
{
arrays[function5(arrays) - 1] = 0;
trailingstop = TimeCurrent() - (arrays[function5(arrays) - 1]);
if(function5(arrays) < 2)
break;
}
}
int function5(int &arrays[50])
{
int vals = ArraySize(arrays);
for(int k = 0; k < vals; k++)
if(!(arrays[k] > 0))
return (k);
return (vals - 1);
}
void function4(double &val[50])
{
int vald = ArraySize(val);
for(int l = vald; l > 0; l--)
val[l] = val[l - 1];
val[0] = 0;
}
void function3(int &arrays[50])
{
int vald = ArraySize(arrays);
for(int l = vald; l > 0; l--)
arrays[l] = arrays[l - 1];
arrays[0] = 0;
}
Arrays start at 0, not 1. When using ArraySize to define counting of arrays, you should always minus 1 to account for starting at 0.
void function6(int &arrays[50])
{
int vals = ArraySize(arrays)-1;
int trailingstop = TimeCurrent() - (arrays[function5(arrays) - 1]);
while(trailingstop > Trigger)
{
arrays[function5(arrays) - 1] = 0;
trailingstop = TimeCurrent() - (arrays[function5(arrays) - 1]);
if(function5(arrays) < 2) break;
}
}
int function5(int &arrays[50])
{
int vals = ArraySize(arrays)-1;
for(int k = 0; k < vals; k++)
if(!(arrays[k] > 0)) return (k);
return (vals - 1);
}
void function4(double &val[50])
{
int vald = ArraySize(val)-1;
for(int l = vald; l > 0; l--)
val[l] = val[l - 1];
val[0] = 0;
}
void function3(int &arrays[50])
{
int vald = ArraySize(arrays)-1;
for(int l = vald; l > 0; l--) arrays[l] = arrays[l - 1];
arrays[0] = 0;
}
Related
ran into a problem regarding fprintf, we want to write a double array into our file for every loop in our main's while loop, however when this loop goes higher than 10 it simply stops fprintf'ing.
The function in question is "store_trash_file". Also the program is controlled by a few defines at top.
Day_max = controls the amount of loops in our while loop
SIZE = with higher SIZE it seems it will fprintf less, and with a smaller it will fprintf more lines.
Ill be putting up the entire code, so u can run it, perhaps keep an eye on the create directories function, as it will make folders and some txts
Any help is much appreciated
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <io.h>
#include <process.h>
#define MARGIN 70
#define MARGIN2 30
#define NAREA 4
#define NSUBAREA 4
#define SIZE 20
#define DAY_MAX 20
#define AREAS_PER_TRUCK 4
#define LOWER1 6
#define LOWER2 8
#define LOWER3 10
#define UPPER1 8
#define UPPER2 10
#define UPPER3 12
typedef struct subarea
{
int co2_cost, time, emptied_subarea_counter, lower_random, upper_random, activity_level;
double average, total_trash_subarea_avg, sensorData[SIZE], sensorDataTotal[SIZE];
} subarea;
typedef struct area
{
subarea *subarea_array[NAREA + 1];
double average, total_trash_area_avg;
int emptied_area_counter;
} area;
void simulated_days(area *area_dynamic, area *area_static);
void average_trash(area *area);
void sensor_data_start(area *area, double start_var);
void compare_trash_to_empty(area *area_array[NAREA + 1], int *area_number_p);
void empty_trash(area *area, int *co2_counter_p, int *time_counter_p);
void store_trash_file(area *area_array[NAREA + 1]);
void create_areas(area *area_array[NAREA + 1]);
void empty_trash_static(area *area, int *co2_counter_static_p, int *time_counter_static_p);
void create_directories();
void make_txt_file(char file_name[30], char file_name2[30]);
int main(void)
{
int co2_counter = 0, time_counter = 0;
int co2_counter_static = 0, time_counter_static = 0;
int day_counter = 0;
int area_number;
int garbage_truck_amount = 0;
double *area_array_avg[DAY_MAX][NAREA];
double *subarea_array_avg[DAY_MAX][NAREA * NSUBAREA];
double *trashcan_array_avg[DAY_MAX][NAREA * NSUBAREA * SIZE];
area *area_array[NAREA + 1];
area *area_array_static[NAREA + 1];
srand(time(NULL));
for (int i = NAREA; i > 0; i -= AREAS_PER_TRUCK)
{
garbage_truck_amount++;
}
printf("Garbage trucks: %d\n", garbage_truck_amount);
create_areas(area_array);
create_areas(area_array_static);
create_directories();
int running = 1;
while (running)
{
for (int i = 1; i < NAREA + 1; i++)
{
simulated_days(area_array[i], area_array_static[i]);
average_trash(area_array[i]);
average_trash(area_array_static[i]);
}
for (int i = 1; i < NAREA + 1; i++)
{
for (int j = 0; j < NSUBAREA; j++)
{
printf("area array: %lf\n", area_array[i]->subarea_array[j]->average);
printf("area array static: %lf\n", area_array_static[i]->subarea_array[j]->average);
}
}
for (int i = 0; i < garbage_truck_amount; i++)
{
compare_trash_to_empty(area_array, &area_number);
empty_trash(area_array[area_number], &co2_counter, &time_counter);
for (int j = 1; j < NAREA + 1; j++)
{
average_trash(area_array[j]);
}
}
if (day_counter > 1 && day_counter % 14 == 0)
{
for (int i = 1; i < NAREA + 1; i++)
{
empty_trash_static(area_array_static[i], &co2_counter_static, &time_counter_static);
average_trash(area_array_static[i]);
}
}
store_trash_file(area_array);
printf("Day: %d\n", day_counter + 1);
for (int i = 0; i < NSUBAREA; i++)
{
for (int j = 1; j < NAREA + 1; j++)
{
printf("%lf\t", area_array[j]->subarea_array[i]->average);
printf("%lf\t", area_array_static[j]->subarea_array[i]->average);
}
printf("\n");
}
printf("\n");
day_counter++;
if(day_counter == DAY_MAX)
{
running = 0;
}
}
}
void simulated_days(area *area_dynamic, area *area_static)
{
for (int i = 0; i < NSUBAREA; i++)
{
for (int j = 0; j < SIZE; j++)
{
int x = ((rand() % (area_dynamic->subarea_array[i]->upper_random - area_dynamic->subarea_array[i]->lower_random + 1)) + area_dynamic->subarea_array[i]->lower_random);
area_dynamic->subarea_array[i]->sensorData[j] += x;
area_dynamic->subarea_array[i]->sensorDataTotal[j] += x;
area_static->subarea_array[i]->sensorData[j] += x;
area_static->subarea_array[i]->sensorDataTotal[j] += x;
}
}
}
void average_trash(area *area)
{
double sum[NSUBAREA];
double sum_total[NSUBAREA];
double area_trash_sum = 0;
double area_trash_sum_total = 0;
for (int i = 0; i < NSUBAREA; i++)
{
sum[i] = 0;
sum_total[i] = 0;
}
for (int i = 0; i < NSUBAREA; i++)
{
for (int j = 0; j < SIZE; j++)
{
sum[i] += area->subarea_array[i]->sensorData[j];
sum_total[i] += area->subarea_array[i]->sensorDataTotal[j];
}
area->subarea_array[i]->average = sum[i] / SIZE;
area->subarea_array[i]->total_trash_subarea_avg = sum_total[i] / SIZE;
}
for (int i = 0; i < NSUBAREA; i++)
{
area_trash_sum += area->subarea_array[i]->average;
area_trash_sum_total += area->subarea_array[i]->total_trash_subarea_avg;
}
area->average = area_trash_sum / NSUBAREA;
area->total_trash_area_avg = area_trash_sum_total / NSUBAREA;
}
void sensor_data_start(area *area, double start_var)
{
for (int i = 0; i < NSUBAREA; i++)
{
for (int j = 0; j < SIZE; j++)
{
area->subarea_array[i]->sensorData[j] = start_var;
area->subarea_array[i]->sensorDataTotal[j] = start_var;
}
}
}
void compare_trash_to_empty(area *area_array[NAREA + 1], int *area_number_p)
{
int highBlock = 0;
for (int i = 1; i < NAREA + 1; i++)
{
if (area_array[i]->average >= MARGIN)
{
if (area_array[i]->average > area_array[highBlock]->average)
{
highBlock = i;
}
}
}
*area_number_p = highBlock;
printf("\nhighblock %d\n", highBlock);
}
void empty_trash(area *area, int *co2_counter_p, int *time_counter_p)
{
for (int i = 0; i < NSUBAREA; i++)
{
if (area->subarea_array[i]->average > MARGIN2)
{
*co2_counter_p += area->subarea_array[i]->co2_cost;
*time_counter_p += area->subarea_array[i]->time;
area->subarea_array[i]->average = 0;
area->subarea_array[i]->emptied_subarea_counter++;
for (int j = 0; j < SIZE; j++)
{
if (area->subarea_array[i]->sensorData[j] <= 100)
{
area->subarea_array[i]->sensorData[j] = 0;
}
else if (area->subarea_array[i]->sensorData[j] > 100)
{
area->subarea_array[i]->sensorData[j] -= 100;
}
}
}
}
area->emptied_area_counter++;
}
void store_trash_file(area *area_array[NAREA + 1])
{
// int xcount = 0;
// int ycount = 0;
// double area_array_avg[NAREA];
// double subarea_array_avg[NAREA * NSUBAREA];
// double trashcan_array_avg[NAREA * NSUBAREA * SIZE];
FILE *output_filepointer;
char *dirname2 = "C:\\Users\\jacob\\Documents\\software\\projekter\\p1\\kode\\intelligenttrash\\data\\area";
char newdirname[150];
char newdirname2[150];
char newdirname3[150];
for (int i = 1; i < NAREA + 1; i++)
{
// area_array_avg[i - 1] = (area_array[i]->total_trash_area_avg);
snprintf(newdirname, 150, "%s%d\\area_average_total.txt", dirname2, i);
output_filepointer = fopen(newdirname, "a");
fprintf(output_filepointer, "%lf ", area_array[i]->total_trash_area_avg);
for (int j = 0; j < NSUBAREA; j++)
{
// subarea_array_avg[j + (ycount * NSUBAREA)] = (area_array[i]->subarea_array[j]->total_trash_subarea_avg);
snprintf(newdirname2, 150, "%s%d%s%d\\subarea_average_total.txt", dirname2, i, "\\subarea", j);
output_filepointer = fopen(newdirname2, "a");
fprintf(output_filepointer, "%lf ", area_array[i]->subarea_array[j]->total_trash_subarea_avg);
for (int z = 0; z < SIZE; z++)
{
// trashcan_array_avg[z + (xcount * SIZE)] = (area_array[i]->subarea_array[j]->sensorDataTotal[z]);
snprintf(newdirname3, 150, "%s%d%s%d\\trashcan_trash.txt", dirname2, i, "\\subarea", j);
output_filepointer = fopen(newdirname3, "a");
fprintf(output_filepointer, "%lf ", area_array[i]->subarea_array[j]->sensorDataTotal[z]);
}
// xcount++;
fprintf(output_filepointer, "\n");
output_filepointer = fopen(newdirname2, "a");
fprintf(output_filepointer, "\n");
}
// ycount++;
output_filepointer = fopen(newdirname, "a");
fprintf(output_filepointer, "\n");
}
fclose(output_filepointer);
}
void create_areas(area *area_array[NAREA + 1])
{
int percentage_added[3][2] = {{LOWER1, UPPER1}, {LOWER2, UPPER2}, {LOWER3, UPPER3}};
int activity_level;
for (int i = 0; i < NAREA + 1; i++)
{
area_array[i] = malloc(sizeof(area));
for (int j = 0; j < NSUBAREA; j++)
{
activity_level = (rand() % 3);
area_array[i]->subarea_array[j] = malloc(sizeof(subarea));
area_array[i]->subarea_array[j]->co2_cost = 50;
area_array[i]->subarea_array[j]->time = 50;
area_array[i]->subarea_array[j]->average = 0;
area_array[i]->subarea_array[j]->emptied_subarea_counter = 0;
area_array[i]->subarea_array[j]->total_trash_subarea_avg = 0;
area_array[i]->subarea_array[j]->lower_random = percentage_added[activity_level][0];
area_array[i]->subarea_array[j]->upper_random = percentage_added[activity_level][1];
area_array[i]->subarea_array[j]->activity_level = activity_level;
}
sensor_data_start(area_array[i], 0);
area_array[i]->average = 0;
area_array[i]->total_trash_area_avg = 0;
area_array[i]->emptied_area_counter = 0;
}
}
void empty_trash_static(area *area, int *co2_counter_static_p, int *time_counter_static_p)
{
for (int i = 0; i < NSUBAREA; i++)
{
*co2_counter_static_p += area->subarea_array[i]->co2_cost;
*time_counter_static_p += area->subarea_array[i]->time;
area->subarea_array[i]->average = 0;
area->subarea_array[i]->emptied_subarea_counter++;
for (int j = 0; j < SIZE; j++)
{
if (area->subarea_array[i]->sensorData[j] <= 100)
{
area->subarea_array[i]->sensorData[j] = 0;
}
else if (area->subarea_array[i]->sensorData[j] > 100)
{
area->subarea_array[i]->sensorData[j] -= 100;
}
}
}
area->emptied_area_counter++;
}
void create_directories()
{
int check;
char *dirname1 = "C:\\Users\\jacob\\Documents\\software\\projekter\\p1\\kode\\intelligenttrash\\data";
char *dirname2 = "C:\\Users\\jacob\\Documents\\software\\projekter\\p1\\kode\\intelligenttrash\\data\\area";
mkdir(dirname1);
for (int i = 1; i < NAREA + 1; i++)
{
char newdirname[100];
char newdirname2[100];
snprintf(newdirname, 100, "%s%d", dirname2, i);
mkdir(newdirname);
for (int j = 1; j < NSUBAREA + 1; j++)
{
snprintf(newdirname2, 100, "%s%d%s%d", dirname2, i, "\\subarea", j);
mkdir(newdirname2);
}
}
}
void make_txt_file(char file_name[30], char file_name2[30])
{
FILE *output_filepointer;
char *dirname2 = "C:\\Users\\jacob\\Documents\\software\\projekter\\p1\\kode\\intelligenttrash\\data\\area";
char newdirname[100];
char newdirname2[100];
for (int i = 1; i < NAREA + 1; i++)
{
if (file_name != "none")
{
snprintf(newdirname, 100, "%s%d\\%s.txt", dirname2, i, file_name);
output_filepointer = fopen(newdirname, "a");
}
for (int j = 1; j < NSUBAREA + 1; j++)
{
if (file_name2 != "none")
{
snprintf(newdirname2, 100, "%s%d%s%d\\%s.txt", dirname2, i, "\\subarea", j, file_name2);
output_filepointer = fopen(newdirname2, "a");
}
}
}
fclose(output_filepointer);
}
I'm currently stuck on cs50's pset4 filter (less comfortable). The program compiles perfectly. I have tried out the first three parts of the code (grayscale, sepia & reflection) and their output has been as desired; however, whenever I try out blur, I keep getting hit with a seg fault. Could someone please help me identify where the error(s) lies?
void blur(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i <height; i++)
{
for (int j = 0; j < width; j++)
{
int c = i + 1;
int d = j + 1;
int e = i + 2;
int f = j + 2;
float blurred_blue = 0;
float blurred_green = 0;
float blurred_red = 0;
int z = 0;
if (height - i == 1 && width - j >= 2)
{
for (int a = i - 1; a < c; a++)
{
for (int b = j - 1; b < f; b++)
{
blurred_blue = blurred_blue + image[a][b].rgbtBlue;
blurred_green = blurred_green + image[a][b].rgbtGreen;
blurred_red = blurred_red + image[a][b].rgbtRed;
z++;
}
}
}
else if (width - j == 1 && height - i >= 2)
{
for(int a = i - 1; a < e; a++)
{
for(int b = j - 1; b < d; b++)
{
blurred_blue = blurred_blue + image[a][b].rgbtBlue;
blurred_green = blurred_green + image[a][b].rgbtGreen;
blurred_red = blurred_red + image[a][b].rgbtRed;
z++;
}
}
}
else
{
for(int a = i - 1; a < e; a++)
{
for(int b = j - 1; b < f; b++)
{
blurred_blue = blurred_blue + image[a][b].rgbtBlue;
blurred_green = blurred_green + image[a][b].rgbtGreen;
blurred_red = blurred_red + image[a][b].rgbtRed;
z++;
}
}
}
image[i][j].rgbtBlue = round (blurred_blue / z);
image[i][j].rgbtGreen = round (blurred_green / z);
image[i][j].rgbtRed = round (blurred_red / z);
}
}
return;
}
I'm programing a 2048 game copy in C. But I can't figure out the game over function. I've this struct:
typedef struct struct_BLOCO
{
int valor;
int cor;
int x, y;
} BLOCO;
And this is the function I give the board its coordinates:
void GiveBlocksCoordinates(BLOCO bloco[16])
{
int i, j, cont = 0;
for (i = 0; i < MAX; i++)
{
for (j = 0; j < MAX; j++)
{
bloco[cont].x = (j * 8) + X_INI;
bloco[cont].y = (i * 4) + Y_INI;
cont++;
}
}
}
And just put them on screen using a function using the values bloco[i].[x]/[y].
This is my "GameOver" function, in its current state:
PS: The "quant" integer recivies the 16 value. If a block is empty, it has 0 value in it. If is "Game Over", the function return 1.
int acabouJogo(BLOCO vec[], int quant)
{
int i, j, x, y, cont = 0, BlocosOcupados = 1;
for (i = 0; i < quant; i++)
if (vec[i].valor != 0)
BlocosOcupados++;
if (BlocosOcupados == quant)
{
for (x = 0; x != 16; x = x + 4)
{
while (cont < 3)
{
if (vec[x + cont].valor != vec[(x + cont) + 1].valor)
cont++;
else
return 0;
}
cont = 0;
}
for (x = 0; x < 4; x++)
{
while ( (cont + x) != (12 + x) )
{
if (vec[x + cont].valor != vec[(x + cont) + 4].valor)
cont = cont + 4;
else
return 0;
}
cont = 0;
}
}
else
return 0;
return 1;
}
Can you guys help me?
Sorry for my english.
Thanks.
I have written a box blur code for gray image blur, but it turned out to work much slower than I expected. How can I improve its performance?
void boxfilter(int* const srcImg, int* const desImg, const int mask, const int nrows, const int ncols)
{
LL *buffer, *rowImg, *rowPtr;
int *srcPtr, *desPtr;
LL pre, sum;
int row, col, i, len, r;
const int MAX_ROW_COL = nrows > ncols ? nrows : ncols;
const int MASK_SIZE = mask*mask;
LL *headL, *tailL;
int *head, *tail;
r = mask / 2;
rowImg = (LL *)malloc(sizeof(LL)*nrows*ncols);
buffer = (LL *)malloc(sizeof(LL)*(MAX_ROW_COL + mask));
len = nrows + 2 * r;
srcPtr = srcImg;
rowPtr = rowImg;
for (col = 0; col < ncols; ++col)
{
srcPtr = srcImg + col;
head = srcPtr + r*ncols;
tail = srcPtr + (nrows - 2 - r)*ncols;
for (i = 0; i < r; ++i)
{
buffer[i] = *head;
buffer[len - 1 - i] = *tail;
head += ncols;
tail -= ncols;
}
for (i = r; i < len - r; ++i)
{
buffer[i] = *srcPtr;
srcPtr += ncols;
}
sum = buffer[0];
for (i = 1; i < mask; ++i)
{
sum += buffer[i];
}
rowPtr = rowImg + col;
*rowPtr = sum;
pre = sum;
headL = buffer;
tailL = buffer + mask;
for (i = mask; i < len; ++i, ++headL, ++tailL)
{
*rowPtr = pre;
rowPtr += ncols;
pre = pre - *headL + *tailL;
}
}
len = ncols + 2 * r;
rowPtr = rowImg;
desPtr = desImg;
for (row = 0; row < nrows; ++row)
{
headL = rowPtr + r;
tailL = rowPtr + (ncols - 2 - r);
for (i = 0; i < r; ++i)
{
buffer[i] = *headL;
buffer[len - 1 - i] = *tailL;
++headL;
--tailL;
}
for (i = r; i < len - r; ++i)
{
buffer[i] = *rowPtr;
++rowPtr;
}
sum = buffer[0];
for (i = 1; i < mask; ++i)
{
sum += buffer[i];
}
*desPtr = sum/MASK_SIZE;
++desPtr;
pre = sum;
headL = buffer;
tailL = buffer + mask;
for (i = mask; i < len; ++i, ++headL, ++tailL)
{
*desPtr = pre/MASK_SIZE;
++desPtr;
pre = pre - *headL + *tailL;
}
}
free(rowImg);
free(buffer);
return;
}
I'm fairly new to programming and i'm having problem with pointers. My code below works with the exception for that my counts doesn't follow with my article number when i sort it. I probably need pointers to get this working but I don't know how.
Can anyone help me?
void printMenu(void)
{
printf("\nMENU:\n");
printf("(D)isplay the menu\n");
printf("(G)enerate inventory\n");
printf("(P)rint inventory\n");
printf("(L)inear search article\n");
printf("(B)inary search article\n");
printf("(I)nsertion sort inventory\n");
printf("B(u)bble sort inventory\n");
printf("(M)erge sort inventory\n");
printf("(Q)uit program\n");
}
void generateInventory(article inventory[], int noOfArticles,
int minArticleNumber, int maxArticleNumber, int maxNoOfArticles)
{
int i, j;
int idCount[] =
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
for (i = 0; i < noOfArticles; i++)
{
inventory[i].id = rand() % (maxArticleNumber - minArticleNumber + 1) +
minArticleNumber;
idCount[inventory[i].id - 1] = idCount[inventory[i].id - 1] + 1;
for (j = 0; j <= i; ++j)
{
if (idCount[inventory[i].id - 1] > 1)
{
inventory[i].id = rand() % (maxArticleNumber + minArticleNumber);
}
}
inventory[i].counts = rand() % maxNoOfArticles;
}
}
void printInventory(const article inventory[], int noOfArticles)
{
int i;
printf("\nINVENTORY\n");
printf("%7s %8s\n", "Article", "Count");
for (i = 0; i < noOfArticles; i++)
{
printf("%7d %8d\n", inventory[i].id, inventory[i].counts);
}
}
int getArticleId()
{
int id;
printf("\nGive article id: ");
scanf("%d", &id);
return id;
}
void printSearchResult(const article inventory[], int index)
{
if (index == -1)
{
printf("\nArticle not found\n");
}
else
{
printf("\nArticle id: %d\n", inventory[index].id);
printf("Article counts: %d\n", inventory[index].counts);
}
}
int linearSearchInventory(const article inventory[], int noOfArticles, int id)
{
int i = 0;
int index = -1;
while (index == -1 && i < noOfArticles)
{
if (id == inventory[i].id)
{
index = i;
}
i++;
}
}
int binarySearchInventory(const article inventory[], int noOfArticles, int id)
{
int index = -1;
int left = 0;
int right = noOfArticles - 1;
int middle;
while (index == -1 && left <= right)
{
middle = (left + right) / 2;
if (id == inventory[middle].id)
{
index = middle;
}
else if (id < inventory[middle].id)
{
right = middle - 1;
}
else
{
left = middle + 1;
}
}
return index;
}
void insertionSortInventory(article inventory[], int noOfArticles)
{
int i, j;
int next;
for (i = 1; i < noOfArticles; i++)
{
next = inventory[i].id;
j = i - 1;
while (j >= 0 && next < inventory[j].id)
{
inventory[j + 1].id = inventory[j].id;
j = j - 1;
}
inventory[j + 1].id = next;
}
}
void bubbleSortInventory(article inventory[], int noOfArticles)
{
int c, d, t;
for (c = 0; c < (noOfArticles - 1); c++)
{
for (d = 0; d < noOfArticles - c - 1; d++)
{
if (inventory[d].id > inventory[d + 1].id)
{
t = inventory[d].id;
inventory[d].id = inventory[d + 1].id;
inventory[d + 1].id = t;
}
}
}
}
void mergeSortInventory(article inventory[], int noOfArticles)
{
int temp[noOfArticles / 2];
int nLeft, nRight;
int i, iLeft, iRight;
if (noOfArticles > 1)
{
nLeft = noOfArticles / 2;
nRight = (int) ceil((double) noOfArticles / 2);
mergeSortInventory(inventory, nLeft);
mergeSortInventory(&inventory[noOfArticles / 2], nRight);
for (i = 0; i < nLeft; i++)
{
temp[i] = inventory[i].id;
}
i = 0;
iLeft = 0;
iRight = 0;
while (iLeft < nLeft && iRight < nRight)
{
if (temp[iLeft] < inventory[noOfArticles / 2 + iRight].id)
{
inventory[i].id = temp[iLeft];
iLeft = iLeft + 1;
}
else
{
inventory[i].id = inventory[noOfArticles / 2 + iRight].id;
iRight = iRight + 1;
}
i = i + 1;
}
while (iLeft < nLeft)
{
inventory[i].id = temp[iLeft];
i = i + 1;
iLeft = iLeft + 1;
}
}
}
If I'm correct in what you're asking, you want to keep the idCount array relational to the inventory array. I assume, since you're using article as a type that you've either typedef'd a variable to be an article, which would be pointless, or more likely you've built a struct of type article, then made an array of those structs, and called the array inventory.
If this is the case, then the most likely method of keeping them relational is to just include the count in the article struct.
There are methods of making the arrays relational without doing that, but they're pointless, because a simple four-line struct would do the trick, even if that struct was a wrapper around a different struct, or a header for another struct.
When sorting your records, you only assign the id member of your struct:
inventory[foo].id = inventory[bar].id;
You should assign the complete struct:
inventory[foo] = inventory[bar];
Just remember that temporaries must be of type article an not int so they allso can be assigne a complete struct and not only an id value