I think the discussion is getting non-academic more than fruitful..we should top here..stop it all please.
I think the discussion is getting non-academic more than fruitful..we should top here..stop it all please.
Tha'ts just closing eyes and ignoring facts..hadn't been you there to see my posts after every adak's sugggestion?..You are infact beating about the bush..Surprised to see mates like you whose job is to just make remember which one is doing what ?(by not helping others/suggesting others ) .
This line?
It doesn't equate to the statement "indentation is nothing." If anything, it shows the opposite.// Formatted using astyle -A4 -U -p -f filename.cpp
Also, in my own personal opinion, someone learning the language should strive for simple readability (e.g. indentation) themselves manually, instead of relying on external tools for this purpose. It's just good practice (in both senses of the word), IMO.
Your solution is not wrong in the sense that the program does what it was intended to do. But that doesn't say a lot. You could replace every loop in the code with ifs and gotos, and the code would still not be wrong in that sense; it would work. It would be easy to justify why such a solution is not satisfactory. And it would certainly not be something to teach newcomers.My solution is not "wrong".
You could solve this problem using only assembly and it would probably provide more "insight into what is happening" than your solution does. But that wouldn't get the OP any closer to a proper C++ program. Here are 2 things that would help in that regard:In fact, my contributions give more insight into what is happening than using vectors can ever hope to do.
Using std::vector.
Writing the code (rather than having it handed on a silver platter)
How I need a drink, alcoholic in nature, after the heavy lectures involving quantum mechanics.
The original poster is new to programming, so I wouldn't expect the code to be very good, just something that works. After he has a working version, he can try to clean it up.
Getting back to the original problem, where's the actual CRC code? You have the user enter a message, then enter a pattern, which you then effectively append to the message. Implementing CRC is usually done similar to long hand division, except that xor is used instead of subtract, and what is appended to the message is the remainder.
So if the CRC "polynomial" has "n+1" bits, producing a "n" bit remainder after division, and a message length of "m" bits, you append "n" zeroes to the message, then divide that message by the CRC "polynomial", then store the remainder into those "n" zeroes that you appended to the message.
remainder = (message x 2^n)%(crc_polynomial)
encoded _message = (message x 2^n) - remainder
Where - is exclusive or, but in this case it's just "n" appended zeroes being xor'ed to so you can just store the remainder.
update - It's not clear to me if the append part of this program was to really do the equivalent of reallocating an array, as opposed to using a large array (like 256 bytes), and then adjusting a variable that represents the length of the message without involving any array reallocation. A second array would be needed for the CRC generator polynomial.
Last edited by rcgldr; 08-21-2013 at 08:17 PM.
If the long term goal is to generate CRC, normally an array large enough to hold the largest message and the redundancy data (remainder) would be used, but since this is a classroom program, maybe the goal of expanding arrays was wanted. Here is an example using realloc, which is a "C" method, but if you going to use an array instead of a vector, then malloc and realloc would be alternatives to using new. Note that realloc preserves the current data when it reallocates memory, but if it's an expansion, the additional memory is uninitialized. This example could be considered to be a C program (versus C++):
Code:#include <malloc.h> #include <stdio.h> int main() { int * pArray; /* pointer to array */ int * pTemp; /* temp ptr */ int i; /* allocate 20 integers */ pArray = malloc(20*sizeof(int)); if(pArray == NULL) return(0); for(i = 0; i < 20; i++) /* put data into the array */ pArray[i] = i; /* reallocate to 30 integers */ pTemp = realloc(pArray, 30*sizeof(int)); if(pTemp == NULL){ /* if realloc failed */ /* this could be a goto the free below, to avoid redundant code */ free(pArray); return(0); } pArray = pTemp; /* update pArray */ for(i = 20; i < 30; i++) /* put more data into the array */ pArray[i] = i; for(i = 0; i < 30; i++){ /* display the array */ printf("%4d ", pArray[i]); if(9 == (i%10)) printf("\n"); } free(pArray); /* free the memory */ return(0); }
Last edited by rcgldr; 08-22-2013 at 12:52 AM.
Seriously, after people in the thread complain in full force that manual reallocation is bad, you come and do the same? And to make matters worse, you use C and you hand out a solution?
We are trying to teach the OP modern C++ here. If the OP was interested in C, he/she would have posted in the C section. Stop posting C code in the C++ forum, please.
Most C code can be compiled as C++, but a lot of C techniques - including ones you used - are considered very poor practice in C++. By using such code in a C++ forum, frankly, you made a fool of yourself. The OP can be forgiven for ignorance on that front, but you were answering the question.
Elysia did fine in terms of supplying suggestions - in mostly natural language - about what was needed. The OP would need to think a little, but will learn significantly more of use by responding to Elysia's hints than by simply copy/pasting the code you supplied.
Anyone who has spent time helping beginners knows that people learn more if they THINK through a problem, and learn almost nothing of use if someone just gives them the code they ask for.
The fact that the OP doesn't want to think through the problem does not excuse you just supplying the code as you did. The fact you provided very poor code just makes your mistake even worse.
And, by persisting in arguing otherwise, you are showing yourself up as an arrogant prat.
You are making a fool of yourself again.
The OP is a newbie. You are not. You are supposed to show how it is properly done instead of copy and pasting some code that you know is not good code. You are supposed to steer newbies to modern C++, not some mix-mash old C.
@jeedi khan: I would (and others, no doubt) still want to look at your vector code, but without that proper indentation, it's just too horrible to read. Do you think you can fix the indentation?