Last edited by std10093; 12-21-2012 at 09:51 AM.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
We shouldn't turn this into a debate about complexity, but doesn't repeatedly shifting the entire array change it from O(n) to O(n2)?
But even ignoring the time complexity, it makes the program itself much more complicated than is necessary.
Oh, and some of us have actually heard of a fellow named Archimedes as well!
Exactly
Of course you do
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Here's my solution (it work's ) but it became quite complex and due to the shifting the garbage value also shows.
Now will think on R.Stiltskin's idea.
I did not implement the size returning function as I am not very satisfied with the garbage values creeping in. Maybe I will modify to store 0/-1 instead of garbage and check conflict if user inputs the same.Code:#include <stdio.h>#include <stdlib.h> #define MAXSIZE 10 int compact(int[]); int main() { int num=0, numarray[MAXSIZE]={0,0,0,0,0,0,0,0,0,0}, i=0; int top = -1; printf("Please enter some numbers. Enter -1 to stop.\n"); while(num != -1) { scanf("%d", &num); if(top==(MAXSIZE-1) || num == -1) { printf("Done !\n"); break; } else { top+=1; numarray[top] = num; } } while(i<MAXSIZE) { printf("%d ", numarray[i]); i++; } printf("\nPrinting compact numvers.\n"); compact(numarray); getchar(); return 0; } int compact(int changeArray[MAXSIZE]) { int i=0, j=0, k=0, l=0; while(l<MAXSIZE) { i=0, j=0; while(i < MAXSIZE) { if(changeArray[i]==changeArray[i+1]) { j=i; while(j < MAXSIZE) { changeArray[j]=changeArray[j+1]; j++; } } i++; } l++; } while(k<MAXSIZE) { printf("%d ", changeArray[k]); k++; } getchar(); return 0; }
Last edited by Debojyoti Das; 12-21-2012 at 01:45 PM.
Spelling
Of course take a shot in R.Stiltskin's idea.Code:printf("\nPrinting compact numvers.\n");
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Nice job. Now why don't you modify the program so it counts (and prints) the number of assignments performed (i.e., how many times a number is moved from one place to another) while processing a given input. Then do the same thing with your previous program and compare the number of operations for the same input array.
Shh... this is the C programming forum, not VBOriginally Posted by Debojyoti Das
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
So, I added a step-counter here and also here:Code:while(l<MAXSIZE) { i=0, j=0; while(i < MAXSIZE) { if(changeArray[i]==changeArray[i+1]) { j=i; while(j < MAXSIZE) { changeArray[j]=changeArray[j+1]; j++; } stepcounter++; } i++; } l++; }
Attachment 12372Code:for(i = 0; i != arrLength; i++) { if(newArray[j] != arrSeries[i]) { newArray[++j] = arrSeries[i]; stepcounter++; }
Attachment 12373
No doubt my previous code was much more efficient than this one. Trying to modify the previous code to make it work dynamically .
Your attachments do not lead me anywhere :/
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
It got deleted for reasons I don't know
Well it's the number of steps my first and second piece of code does for similar set of numbers. In fact the second piece of code was provided a number less yet it took substantially more steps to compute than the first step, which did the computation with one number more in the series in so less steps. Sorry for being ambiguous in my previous post .1 2 1 2 3 1
The number of steps: 5
1 1 1 2 2 1 3 3 1 1
Printing compact numbers.
1 2 1 3 1 1 1 1 1 1
Steps Performed: 63
Last edited by Debojyoti Das; 12-22-2012 at 06:59 AM.
No problem
So, can you post the code that you think is the best you have until now?
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
This one, but it is highly restricted as it works only with a static data set.Code:#include <stdio.h>#include <stdlib.h> void compact (int[], int); int main() { int arrSeries[11] = {1, 1, 1, 2, 2, 1, 2, 3, 3, 1, 1 }; int arrLength = 11; compact(arrSeries, arrLength); getchar(); return 0; } void compact(int arrSeries[], int arrLength) { int i, j= 0, counter=0, newArray[11], stepcounter = 0; newArray[0]=arrSeries[0]; for(i = 0; i != arrLength; i++) { if(newArray[j] != arrSeries[i]) { newArray[++j] = arrSeries[i]; stepcounter++; } } while(counter !=6) { printf("%d ", newArray[counter]); counter++; } printf("\nThe number of steps: %d", stepcounter); }
Only one for loop in the function which is good. Well do you really need stepcounter? I would say that j has the same value with stepcounter when you reach line 31.
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson