Pointer notation for insertion sorting using C -



Pointer notation for insertion sorting using C -

objective: i'm working pointers , i'm running quite few problems. lastly week assignment write insertion sort function sort ragged array in descending order. week professor wants me alter out indices , utilize pointers.

void insert(int **table, int row) { // local declaration int **ptr = table; int **walkplus, *walk, temp; // statement for(ptr = (table + 1); *ptr != null; ptr++) { temp = **ptr; walk = *(ptr - 1); while(*walk >= diff && temp > *walk) { walkplus = ptr; **walkplus = *walk; *walk--; } **walkplus = temp; printf("\n"); } return; }

i sense *walk--; cause problem. when utilize printf statement check it's value, i'm getting weird address. know pointers of import , want create sure understand concept, help appreciated. give thanks you.

#include <stdio.h> #include <stdlib.h> #include <time.h> #include <ctype.h> #ifdef _msc_ver #include <crtdbg.h> // needed check memory leaks (windows only!) #endif #define mem_error printf("not plenty memory\n") int** getrow(int *row); void valirow(int *row); void getsize(int **table, int row); void valisize(int *size); void filltable(int **table, int row); void bubble(int **table, int row); void insert(int **table, int row); void freealo(int **table); int main (void) { // local declaration int **table; int row, i; char answer; int **ptable; file* fpoutput; // statement fpoutput = fopen("output.txt", "w"); if(fpoutput == null) { printf("error, writing failed.\n"); exit(103); } { table = getrow(&row); getsize(table, row); filltable(table, row); bubble(table, row); insert(table, row); freealo(table); printf("\ndo want create new ragged table? "); printf("[y] continue: "); scanf(" %c", &answer); printf("\n"); } while(toupper(answer) == 'y'); fclose(fpoutput); #ifdef _msc_ver printf( _crtdumpmemoryleaks() ? "memory leak\n" : "no memory leak\n"); #endif homecoming 0; }// main /* getrow */ int** getrow(int *row) { // local declaration int **table; // statement printf("please come in number of rows (1-10): "); scanf("%d", &*row); valirow(&*row); table =(int**)calloc(*row + 1, sizeof(int)); if(table == null) mem_error, exit(100); homecoming table; } /* valirow */ void valirow(int *row) { // statement while(*row > 10 || *row < 1) { while(getchar() != '\n') ; printf("please come in number between (1-10): "); scanf("%d", &*row); } return; } /* getsize */ void getsize(int **table, int row) { // local declaration int size; int **ptr = table; int **plast = table + row; // statement ptr = table; for( ; ptr < plast; ptr++) { printf("please come in size (1-15): "); scanf("%d", &size); valisize(&size); *ptr = (int*)calloc(size + 1, sizeof(int)); **ptr = size; } if(table == null) mem_error, exit(101); return; } /* valisize */ void valisize(int *size) { // statement while(*size > 15 || *size < 1) { while(getchar() != '\n') ; printf("please come in valid size (1-15): "); scanf("%d", &*size); } return; } /* filltable */ void filltable(int **table, int row) { // local declaration int random; int **ptr = table; int *pcurr, *pwalk; // statement srand(time(null)); for(pcurr = *ptr ; *ptr != null; ptr++) { for(pwalk = (pcurr + 1); *pwalk < *pcurr; pwalk++) { random = -99 + rand() % 199; *pwalk = random; } pcurr = *(ptr + 1); } return; } /* bubble */ void bubble(int **table, int row) { // local declaration int **ptr; int *pwalk; int temp, target; // statment for(ptr = table; *ptr != null; ptr++) { for(target = **ptr; target > 0; target--) { for(pwalk = *ptr + target; pwalk != *ptr + 1; pwalk--) { if(*pwalk > *(pwalk - 1)) { temp = *pwalk; *pwalk = *(pwalk - 1); *(pwalk - 1) = temp; } } } } return; } /* insert */ void insert(int **table, int row) { // local declaration int **ptr = table; int temp, *walk, **walkplus; // statement for(ptr = (table + 1); *ptr != null; ptr++) { temp = **ptr; walk = *(ptr - 1); while(*walk >= 0 && temp > *walk) { walkplus = ptr; **walkplus = *walk; *walk--; } **walkplus = temp; } return; } /* freealo */ void freealo(int **table) { // local declaration int ** ptr; // statement for(ptr = table; *ptr != null; ptr++) { free(*ptr); } free(ptr); return; }

post prepare operator -- has higher precedence unary operator *. need take care of that. have overlooked it? please precedence table in next link

shortcut remember order of evaluation , precedence of operators in c

c

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -