I have to read a maze from a file and store it in a twodimensional array.
The characters I'm reading are stored in a .txt file like this:
######
#....#
#..#.#
. .#..
######
Note that the number of rows and columns can vary depending on the file.
My approach in reading the file so far:
#include <stdio.h>
#include <stdlib.h>
void read_arr(char** a, int x_size, int y_size) {
int i, j;
int tmp;
FILE* file = fopen("lab1.txt", "r");
for (i = 0; i < y_size; i++) {
for (j = 0; j < x_size; j++) {
if (tmp = fgetc(file))
a[j][i] = tmp;
printf("Success\n");
}
}
}
void print_arr(char** a, int x_size, int y_size) {
int i, j;
for (i = 0; i < x_size; i++) {
for (j = 0; j < y_size; j++) {
printf("%c", a[i][j]);
}
printf("\n");
}
}
int main() {
int x_size, y_size;
printf("What is the size of the maze (<x> <y>)? ");
scanf("%d %d", &x_size, &y_size);
printf("Maze has size %dx%d\n", x_size, y_size);
char** a = malloc(sizeof(char) * (x_size * y_size));
if (!a)
return -1;
printf("Successfully allocated memory!\n");
read_arr(a, x_size, y_size);
print_arr(a, x_size, y_size);
return 0;
}
But all I get from this is a memory error (I'm afraid I cant't give the exact error message, because it is displayed in german).
Another thing I've tried is using fscanf, but that didn't work either.
I'm on Ubuntu, and using gcc to compile my code.
Any help would be much appreciated!
Memory allocation is not correct
char** a = malloc(sizeof(char) * (x_size * y_size));
I guess what you wanted to do is
char** a = malloc(sizeof(char*) * y_size);
for(i = 0; i < y_size; ++i)
a[i]=malloc(x_size);
Also in read_arr function, you access array as arr[j][i], while j is your inner index, and i is outer
for (i = 0; i < y_size; i++) {
for (j = 0; j < x_size; j++) {
if (tmp = fgetc(file))
a[j][i] = tmp; ==> a[i][j] = tmp;
printf("Success\n");
}
}
Related
I'm trying to implement a count sort algorithm in c but I'm getting this "core dumped" compiler message. Does anyone know where my mistake is?
I need to sort an array filled with numbers from an external file.
MY CODE:
#include <stdio.h>
#include <stdlib.h>
#include "arrayio.h"
int MAX_LAENGE = 1000;
int MAX_VALUE = 100;
void count_sort_calculate_counts(int input_array[], int len, int count_array[])
{
count_array = (int*)calloc(MAX_VALUE, sizeof(int));
for(int i = 0; i < MAX_VALUE; i++)
{
count_array[i] = 0;
}
for(int j = 0; j <= len; j++)
{
count_array[input_array[j]] = count_array[input_array[j]] +1;
}
}
void count_sort_write_output_array(int output_array[], int len, int count_array[])
{
int k = 0;
for(int i = 0; i < MAX_VALUE; i++)
{
for(int j = 0; j < count_array[i]; j++)
{
output_array[k] = i;
k = k + 1;
}
}
}
int main(int argc, char *argv[]) {
if (argc < 2){
printf("Aufruf: %s <Dateiname>\n", argv[0]);
printf("Beispiel: %s zahlen.txt\n", argv[0]);
exit(1);
}
char *filename = argv[1];
int input_array[MAX_LAENGE];
int len = read_array_from_file(input_array, MAX_LAENGE, filename);
printf("Unsortiertes Array:");
print_array(input_array, len);
int count_array[MAX_VALUE];
int output_array[MAX_LAENGE];
count_sort_calculate_counts(input_array, len, output_array);
count_sort_write_output_array(output_array, len, count_array);
printf("Sortiertes Array:");
print_array(output_array, len);
free(count_array);
return 0;
}
I thought about it for days but I can't solve it.
this loop:
for(int j = 0; j <= len; j++)
{
count_array[input_array[j]] = count_array[input_array[j]] +1;
}
goes 1 int too far so reads input_array[j] out of input_array bounds. Reading one more rarely triggers a crash, but the value is completely random & possibly very high so count_array[input_array[j]] accesses a very far address, very much out of bounds, hence the crash.
fix proposal (& small simplification):
for(int j = 0; j < len; j++)
{
count_array[input_array[j]] ++;
}
First thing, why do you need to allocate memory dynamically?
count_array = (int*)calloc(MAX_VALUE, sizeof(int));
You only need to allocate memory dynamically if you don't have an idea how much memory is required. Thats the case when you'd use calloc() and malloc() primarily.
In main() function,
int count_array[MAX_VALUE];
This has already allocated memory (Size = MAX_VALUE x size of int) for count_array compile-time. So, you can get rid of the code which involves calloc().
If you want to initialize all array contents to 0, then you can use:
int count_array[MAX_VALUE] = {0};
Secondly, there is an array going out of bounds:
for(int j = 0; j <= len; j++)
Change it to
for(int j = 0; j < len; j++)
when the code is going to change where a pointer, in the calling function, is pointing via some activity in a called function, then the pointer to the called function must be passed as the address of the pointer.
Similar to:
in function: main()
int *output_array = NULL;
count_sort_calculate_counts(input_array, len, &output_array);
in function: count_sort_calculate_counts()
void count_sort_calculate_counts(int input_array[], int len, int **count_array)
*count_array = calloc(MAX_VALUE, sizeof(int));
if( !(*count_array) )
{
// handle error then exit program
}
this code block:
for(int j = 0; j <= len; j++)
{
count_array[input_array[j]] = count_array[input_array[j]] +1;
}
can be greatly simplified to:
for(int j = 0; j <= len; j++)
{
(*count_array)[input_array[j]] += 1;
}
What my program does is that it takes an array of numbers that were read in from a file and sorts them with the selection sort and bubble sort methods. When it's sorted via the bubble sort method, the array lists one number twice in a row. It's also always the second number that gets copied. I checked to see if for whatever reason the number was actually getting passed to the new array twice, but it isn't. I also tried a different input file, same thing happens in the same place.This also cuts off the last number in the list. I don't see anything obvious in my code that's causing that to happen.
The list that the program calls is as follows:
10
50
78
83
92
100
0
4
72
3
19
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int findMin(int arr[], int start, int size);
void printArr(int arr[], int size);
void selectionSort(int arr[], int size);
void bubbleSort(int arr[], int size);
int main(int argc, char *argv[])
{
if(argc != 2)
{
printf("Syntax Error: ./<exec> <infile>\n");
exit(1);
}
FILE *ifp = NULL;
ifp = fopen(argv[1], "r");
if(ifp == NULL)
{
printf("Could not open %s for reading\n", argv[1]);
exit(1);
}
int counter;
int j = 0;
fscanf(ifp, "%d", &counter);
int array[counter];
int arrB[counter];
for(j = 0; j < counter; ++j)
{
fscanf(ifp, "%d", &array[j]);
}
for(j = 0; j < counter; j++)
{
arrB[j] = array[j];
}
int size = sizeof(array) / sizeof(int);
printf("Before: ");
printArr(array, size);
selectionSort(array, size);
bubbleSort(arrB, size);
fclose(ifp);
return 0;
}
int findMin(int arr[], int start, int size)
{
int i = 0;
int minLoc = start;
int minVal = arr[minLoc];
for ( i = start + 1; i < size; ++i)
{
if (arr[i] < minVal)
{
minVal = arr[i];
minLoc = i;
}
}
return minLoc;
}
void printArr(int arr[], int size)
{
int i = 0;
for(i = 0; i < size; ++i)
printf("%3d ", arr[i]);
printf("\n");
}
void selectionSort(int arr[], int size)
{
int i = 0;
int minLoc = 0;
int tmp = 0;
for(i = 0; i < size; ++i)
{
minLoc = findMin(arr, i, size);
tmp = arr[i];
arr[i] = arr[minLoc];
arr[minLoc] = tmp;
}
printf("** Selection Sort **\n After: ");
printArr(arr, size);
}
void bubbleSort(int arr[], int size)
{
int i = 0;
int j = 0;
int tmp = 0;
for(j = 0; j < size; j++)
{
for(i = 0; i < size; ++i)
{
if(arr[i] > arr[i+1])
{
tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
}
}
}
printf("** Bubble Sort **\n After: ");
printArr(arr, size);
}
bubbleSort() accesses outside array bounds. Thus undefined behavior (UB).
for(j = 0; j < size; j++) {
for(i = 0; i < size; ++i) {
if(arr[i] > arr[i+1]) { // Here code access outside bounds when i = size - 1
... Code swaps arr[i], arr[i+1];
}
}
}
Instead
for(j = 0; j < size; j++) {
for(i = 1; i < size; ++i) {
if(arr[i-1] > arr[i]) {
... Code swaps arr[i-1], arr[i];
}
}
}
While compiling in the terminal I keep getting the error Segmentation Fault: 11. The goal is to make dark circles on a picture of a boarder, ect with command. My reasoning that it isn't working if because of my File IO. I did it without the in & out FILE types and changed the two functions that are called in pgmUtility to not call in files and the program ran smoothly. So I'm assuming I need to have help focusing on the issues I have with my file IO.
Command used:
$ ./a.out -c 470 355 100 < balloons.ascii.pgm > TestImages/balloons_c100_4.pgm
It uses main.c program that relates to pgmUtility.c
This is Main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "pgmUtility.h"
#define ROWS 4
#define COLS 100
void usage(void)
{
printf("Usage\n");
printf(" -h Help Dialog\n");
printf(" -e edgeWidth < OldImageFile > NewImageFile\n");
printf(" -c centerRow centerCol radius < OldImageFile > NewImageFile\n");
printf(" -e edgeWidth -c radius centerRow centerCol < OldImageFile > NewImageFile\n");
exit (8);
}
int main(int argc, char * argv[]) {
FILE *fp;
FILE *out;
int i, j;
int flag1 = 0; //-e switch (edge draw)
int flag2 = 0; //-c switch (circle draw)
int numRows, numCols, centerRow, centerCol, radius, edgeWidth;
char originalImage[100], newImageFile[100];
char **header = (char**) malloc (sizeof(char*)*4);
int **pixels;
//command line argument parsing
//turn flag switches on or off
if(argc < 3)
usage();
if(argc > 7)
usage();
for(i = 1; i < argc; i++) {
if(strncmp(argv[i], "-e", 2) == 0) {
//set flag on
//get edge with values)
if(atoi(argv[i+1]) == 0) {
usage();
}
edgeWidth = atoi(argv[i+1]);
if(argv[i+2] != NULL) {
if(atoi(argv[i+2]) != 0) {
usage();
}
}
flag1 = 1;
}
if(strncmp(argv[i], "-c", 2) == 0) {
//set flag on
//get radius and center values
if(atoi(argv[i+1]) == 0) {
usage();
}
centerRow = atoi(argv[i+1]);
centerCol = atoi(argv[i+2]);
radius = atoi(argv[i+3]);
flag2 = 1;
strcpy(originalImage, argv[5]);
strcpy(newImageFile, argv[6]);
fp = fopen(originalImage, "r");
out = fopen(newImageFile, "w");
}
if(strncmp(argv[i], "-h", 2) == 0) {
usage();
}
}
//allocate memory for header array
header = (char **)malloc(ROWS * sizeof(char));
for(i = 0; i < ROWS; i++) {
for(j = 0; j < COLS; j++) {
header[i] = (char *)malloc(COLS * sizeof(char *));
}
}
//read pgm file
pixels = pgmRead(header, &numRows, &numCols, fp);
if(pixels == NULL)
usage();
switch(flag1) {
case 1 :
if(flag2 == 1) {
//execute circle draw and edge draw
pgmDrawCircle(pixels, numRows, numCols, centerRow, centerCol, radius, header);
pgmDrawEdge(pixels, numRows, numCols, edgeWidth, header);
}
else {
//execute only edge draw only
pgmDrawEdge(pixels, numRows, numCols, edgeWidth, header);
}
break;
case 0 :
if(flag2 == 1) {
//execute circle draw
pgmDrawCircle(pixels, numRows, numCols, centerRow, centerCol, radius, header);
}
break;
default :
usage();
break;
}
//write new pgm file
pgmWrite((const char **)header, (const int **)pixels, numRows, numCols, out);
//Garbage Collection
//Fix this
//free(pixels);
//free(header);
for(i = 0; i < numRows; i++) {
int *current= pixels[i];
free(current);
}
for(i = 0; i < ROWS; i++) {
char *current = header[i];
free(current);
}
return 0;
}
This is two functions from pgmUtility.c that I think may be the cause of the issue.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "pgmUtility.h"
#define ROWS 4
#define COLS 100
// Implement or define each function prototypes listed in pgmUtility.h file.
// NOTE: You can NOT change the input, output, and argument type of the functions in pgmUtility.h
// NOTE: You can NOT change the prototype (signature) of any functions listed in pgmUtility.h
int ** pgmRead( char **header, int *numRows, int *numCols, FILE *in ){
int r, c;
int **array;
for(r = 0; r < ROWS; r++) {
fgets(header[r], COLS, stdin);
if(header == NULL)
return NULL;
}
//sscanf parses the numRows and numCols
sscanf(header[ROWS - 2], "%d %d", numCols, numRows);
//read in pixel map
array = (int **)malloc(*numRows * sizeof(int *));
for(r = 0; r < *numRows; r++) {
array[r] = (int *)malloc(*numCols * sizeof(int));
}
for(r = 0; r < *numRows; r++) {
for(c = 0; c < *numCols; c++) {
fscanf(in, "%d", *(array + r) + c );
}
}
fclose(in);
return array;
}
int pgmWrite( const char **header, const int **pixels, int numRows, int numCols, FILE *out ){
//iterate straight through pixels
//setup with a loop to insert a new line every "numCols" and keep printing until "numRows + 1" is reached (as soon as numRows + 1 break loop)
int i, j;
for(i = 0; i < 4; i++){
//printf("%s", *header[i]);
fprintf(out, "%c", *header[i]);
}
//for(i = 0; i < 4; i++)
//fprintf(out, "*I=%d**%s**", i, header[i]);
for(j = 0; j < numRows; j++){
for(i = 0; i < numCols; i++)
fprintf(out, "%d ", pixels[i][j]);
fprintf(out, "\n");
}
fclose(out);
return 0;
}
You're not allocating the header arrays correctly. It should be:
header = malloc(ROWS * sizeof(char*));
for(i = 0; i < ROWS; i++) {
header[i] = malloc(COLS * sizeof(char));
}
You had the wrong types in the two sizeof calls.
You didn't need the inner j loop at all, you were repeatedly assigning to the same header[i].
And for C (but not C++) see: Do I cast the result of malloc?
Also, at the beginning of main() you have an extra allocation that you never use or free:
char **header = (char**) malloc (sizeof(char*)*4);
You should get rid of this.
It's not related to the error, but this is wrong:
if(header == NULL)
return NULL;
You should be testing header[r].
For clarity, I recommend rewriting:
fscanf(in, "%d", *(array + r) + c );
as:
fscanf(in, "%d", &array[r][c]);
It appears (even after correcting the malloc call as correctly suggested), you are allocating header[i] COLS number of times.
header = malloc(ROWS * sizeof(char*));
for(i = 0; i < ROWS; i++) {
for(j = 0; j < COLS; j++) {
header[i] = malloc(COLS * sizeof(char)); // this happens COLS times
}
}
That will leave COLS number of each header[i] allocated. As I read your code, you need only allocate a single char array for each header[i]. To do this, you need to move header[i] = malloc(COLS * sizeof(char)); outside the for(j = 0; j < COLS; j++) loop:
header = malloc(ROWS * sizeof(char*));
for(i = 0; i < ROWS; i++) {
header[i] = malloc(COLS * sizeof(char));
}
You should also validate that header and each header[i] were successfully allocated.
I've been working on this for days but can't seem to make it work out.
Sorry in advance for the unholy length of this, so if anyone takes the time to go through it and try to understand this mess, I'd owe you.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct cart {
int id;
char *nume;
} cart;
typedef struct pach {
int id, idCartier, strada, numar, prioritate, codificare;
float greutate;
char* mesaj;
int adresa[18];
} pach;
typedef struct post {
int id, nrPachete;
int vector[50];
} post;
int citeste(int *nrP, cart *cartier, pach *pachet, int *nrC) {
printf("Punctul 1\n");
int i, j;
scanf("%d", nrC);
for (i = 0; i < *nrC; i++) {
cartier[i].id = i;
char aux[500];
scanf("%s", aux);
cartier[i].nume = malloc(strlen(aux) + 1);
cartier[i].nume = aux;
printf("%d %s\n", cartier[i].id, cartier[i].nume);
}
scanf("%d", nrP);
for (i = 0; i < *nrP; i++) {
pachet[i].id = i;
char aux[500];
for (j = 0; j < 18; j++)
scanf("%d", &pachet[i].adresa[j]);
scanf("%d %f", &pachet[i].prioritate, &pachet[i].greutate);
getchar();
fgets(aux, 256, stdin);
pachet[i].mesaj = malloc(strlen(aux) + 1);
pachet[i].mesaj = aux;
printf("%d\n", pachet[i].id);
for (j = 0; j < 18; j++)
printf("%d ", pachet[i].adresa[j]);
printf("\n%d %.6f ", pachet[i].prioritate, pachet[i].greutate);
printf("%s", pachet[i].mesaj);
}
return *nrP;
}
void extrage(int *nrP, pach *pachet) {
printf("\nPunctul 2\n");
int i, j;
for (i = 0; i < *nrP; i++) {
pachet[i].idCartier = 0;
pachet[i].strada = 0;
pachet[i].numar = 0;
for (j = 0; j < 5; j++)
pachet[i].idCartier += pachet[i].adresa[j] * pow(2, (4 - j));
for (j = 5; j < 10; j++)
pachet[i].strada += pachet[i].adresa[j] * pow(2, (9 - j));
for (j = 10; j < 18; j++)
pachet[i].numar += pachet[i].adresa[j] * pow(2, (17 - j));
printf("%d %d ", pachet[i].id, pachet[i].idCartier);
printf("%d %d\n", pachet[i].strada, pachet[i].numar);
}
}
void distribuie(int *nrP, pach *pachet, post *postas, int *nrC, cart *cartier) {
printf("Punctul 3\n");
int i, j;
for (i = 0; i < *nrC; i++) { // FOR-1A
postas[i].nrPachete = 0;
postas[i].id = i;
for (j = 0; j < 50; j++)
postas[i].vector[j] = 0;
}
for (i = 0; i < *nrC; i++) { // FOR-1B
for (j = 0; j < *nrP; j++) {
if (cartier[i].id == pachet[j].idCartier) {
postas[i].vector[postas[i].nrPachete] = pachet[j].id;
postas[i].nrPachete++;
}
}
printf("%d %d ", postas[i].id, postas[i].nrPachete);
for (j = 0; j < postas[i].nrPachete; j++)
printf("%d ", postas[i].vector[j]);
printf("\n");
}
}
void ordoneaza(pach *pachet, int *nrC, post *postas) {
printf("Punctul 4\n");
pach aux;
int i, j, k = 0, schimbat = 1;
for (i = 0; i < *nrC; i++) {
while (schimbat) {
schimbat = 0;
for (j = 0; j < postas[i].nrPachete - k; j++)
if (pachet[postas[i].vector[j]].prioritate < pachet[postas[i].vector[j+1]].prioritate) {
aux = pachet[postas[i].vector[j]];
pachet[postas[i].vector[j]] = pachet[postas[i].vector[j+1]];
pachet[postas[i].vector[j+1]] = aux;
schimbat = 1;
}
k++;
}
k = 0;
schimbat = 1;
for (j = 0; j < postas[i].nrPachete; j++) {
for (k = j; k < postas[i].nrPachete; k++) {
if (pachet[postas[i].vector[j]].prioritate == pachet[postas[i].vector[k]].prioritate)
if (pachet[postas[i].vector[j]].greutate < pachet[postas[i].vector[k]].greutate) {
aux = pachet[postas[i].vector[j]];
pachet[postas[i].vector[j]] = pachet[postas[i].vector[k]];
pachet[postas[i].vector[k]] = aux;
}
}
}
}
for (i = 0; i < *nrC; i++)
for (j = 0; j < postas[i].nrPachete; j++) {
postas[i].vector[j] = pachet[postas[i].vector[j]].id;
}
for (i = 0; i < *nrC; i++) {
printf("%d %d ", postas[i].id, postas[i].nrPachete);
for (j = 0; j < postas[i].nrPachete; j++)
printf("%d ", postas[i].vector[j]);
printf("\n");
}
}
int main() {
int nrP, nrC;
pach pachet[1600];
post postas[32];
cart cartier[32];
citeste(&nrP, &cartier[32], &pachet[1600], &nrC);
extrage(&nrP, &pachet[1600]);
distribuie(&nrP, &pachet[1600], &postas[32], &nrC, &cartier[32]);
ordoneaza(&pachet[1600], &nrC, &postas[32]);
return (0);
}
Short info on what the program does:
The citeste function should read the cartier and pachet structures. All of them. And then print those in a bit different format.
The extrage function should take every pachet, and use the adresa (written in BINARY) to convert its 3 parts and obtain the strada, numar and idCartier. Then also print those.
Distribuie checks if the pachet is distributed to a postas (distributed means pachet.idCartier == postas.id), if not it distributes it.
Ordoneaza takes every postas's vector and sorts it after the prioritate (or greutate if the prioritate-s are equal).
But it doesn't work as intended and also gives weird Segmentation Faults.
For example if I comment out the distribuie function, it gives me segfault right after extrage. If I put it back, it gives segfault right after doing it. And if I uncomment everything, it gives segfault at the end again.
If anyone actually read all of this and would be willing to reply, I'd highly appreciate it. Any bit of advice helps!
I did not read your code, but your title said you had trouble passing array of structures. I am attaching a working snippet hope it will help you get around your problem.
#include<stdio.h>
typedef struct employee{
int empId;
char name[10];
}EMP;
void arrayOfStruct(EMP *a, int size)
{
printf("%d\t%d",a[0].empId,a[3].empId);
}
int main()
{
EMP NC[4];
NC[0].empId = 9;
NC[3].empId = 2;
arrayOfStruct(&NC[0],sizeof(NC)/sizeof(NC[0]));
}
with the help of size you can never go beyond the memory allocated for structures.
In case you, want to pass higher dimensional arrays, you have to hard code all the size of arrays except the outer most.
void arrayOfStruct(EMP a[][4], int size)
{
// to do
}
int main()
{
EMP NC[2][4];
...
arrayOfStruct(NC,sizeof(NC)/sizeof(NC[0]));
}
as you see, I did not specify the higher most size of array, which I am passing via other arguement.
Why do I need to specify size of inner dimensions ?
Lets take an example, for suppose you have int[4][4], and you are trying to pass array to a function via int[3][], how does a compiler know how many inner blocks to create, in other case via int[][3], the compiler can easily understand that it has to make inner block of size 3 for each outer array.
I am trying to create C program to read a text file and sort it by ascending order. The example of text file is
2
3; 2, 5, 7
6; 4, 7, 8, 9, 5, 2
with the first line indicated the number of rows, the number after the ";" indicated elements each rows and elements separated by ",".
So my idea is to create a dynamic jagged array with rows as the first number, then point each row to the different array with element. Sort the pointer arrays first then sort elements of each arrays. This is what I have tried so far
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int SortLists();
int main ()
{
int i,j = 0;
char filename[10]; //name of the file
char line[100];
int rows = 3; //I have to initialized this to test my code first
int cols;
int **jaggedArr; //jagged array
jaggerArr = malloc (rows*sizeof(int*)) ;
printf("Enter the file name with .txt : ");
scanf("%s", filename);
FILE *filePtr = fopen(filename, "r");
int num;
if (filePtr != NULL)
{
while (fgets(line, sizeof(line), filePtr) != NULL) //read each line of the text
{
cols = atoi(strtok(line, ";")); //use strtk to break elements
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
jaggedArr[i][j] = atoi(strtok(line, ",")); //parse into the jagged array
}
}
}
fclose(filePtr);
}
}
int SortLists(int list[], int size) //sort method
{
int i,j,temp;
for (i = 0; i < size; ++i)
{
for (j = i + 1; j < size; ++j)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
As a beginner in C, I am not familiar with the idea of pointer, which a lot different with C#.
Sorry for my bad English as its not my first language. Thank you so much for helping me.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define S_(x) #x
#define S(x) S_(x)
void SortLists(int list[], int size);
int main(void){
char filename[FILENAME_MAX+1];
char line[100];
int rows, cols;
printf("Enter the file name with .txt : ");
scanf("%" S(FILENAME_MAX) "[^\n]%*c", filename);
FILE *filePtr = fopen(filename, "r");
if(!filePtr)
return EXIT_FAILURE;
fscanf(filePtr, "%d ", &rows);
int **jaggedArr;
jaggedArr = malloc (rows * sizeof(int*));
int *sizeArr = malloc(rows * sizeof(int));
int r = 0, c;
while (fgets(line, sizeof(line), filePtr) != NULL){
sizeArr[r] = cols = atoi(strtok(line, ";"));
jaggedArr[r] = malloc(cols * sizeof(int));
for (c = 0; c < cols; ++c){
jaggedArr[r][c] = atoi(strtok(NULL, ","));
}
SortLists(jaggedArr[r++], cols);
}
fclose(filePtr);
//check print and deallocation
for(r = 0;r < rows; ++r){
for(c = 0; c < sizeArr[r]; ++c)
printf("%d ", jaggedArr[r][c]);
printf("\n");
free(jaggedArr[r]);
}
free(jaggedArr);
free(sizeArr);
return 0;
}
void SortLists(int list[], int size){
int i,j,temp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (list[i] > list[j]){
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
for extra question.
#include <stdio.h>
void SortLists(int list[], int size);
void SortRows(int *jaggedArr[], int size, int *rowSize);
int main(void){
int row1[] = {4,7,8,9,5,2};
int row2[] = {2,5,7};
int *jaggedArr[] = { row1, row2};
int rows = 2;
int sizeArr[] = {6,3};
int i, r, c;
for(i=0;i<rows;++i)
SortLists(jaggedArr[i], sizeArr[i]);
for(r = 0;r < rows; ++r){
for(c = 0; c < sizeArr[r]; ++c)
printf("%d ", jaggedArr[r][c]);
printf("\n");
}
printf("\n");
SortRows(jaggedArr, rows, sizeArr);
for(r = 0;r < rows; ++r){
for(c = 0; c < sizeArr[r]; ++c)
printf("%d ", jaggedArr[r][c]);
printf("\n");
}
return 0;
}
void SortLists(int list[], int size){
int i,j,temp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (list[i] > list[j]){
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
void SortRows(int *jaggedArr[], int size, int *rowSize){
int i,j,temp,*tempp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (rowSize[i] > rowSize[j]){
//swap in pairs
temp = rowSize[i];
rowSize[i] = rowSize[j];
rowSize[j] = temp;
tempp = jaggedArr[i];
jaggedArr[i] = jaggedArr[j];
jaggedArr[j] = tempp;
}
}
}
}
#include <stdio.h>
void SortLists(int list[], int size);
void SortRows(int *jaggedArr[], int size);
int main(void){
int row1[] = {6, 4,7,8,9,5,2};//The first element represents the number of elements.
int row2[] = {3, 2,5,7};
int *jaggedArr[] = { row1, row2};
int rows = 2;
//int sizeArr[] = {6,3};//Not required
int i, r, c;
for(i=0;i<rows;++i)
SortLists(jaggedArr[i]+1, jaggedArr[i][0]);
SortRows(jaggedArr, rows);
for(r = 0;r < rows; ++r){
for(c = 1; c <= jaggedArr[r][0]; ++c)
printf("%d ", jaggedArr[r][c]);
printf("\n");
}
return 0;
}
void SortLists(int list[], int size){
int i,j,temp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (list[i] > list[j]){
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}
void SortRows(int *jaggedArr[], int size){
int i,j,*tempp;
for (i = 0; i < size-1; ++i){
for (j = i + 1; j < size; ++j){
if (jaggedArr[i][0] > jaggedArr[j][0]){
tempp = jaggedArr[i];
jaggedArr[i] = jaggedArr[j];
jaggedArr[j] = tempp;
}
}
}
}