vector works with any type*. It's a generic dynamic array.
So switch your dangerous C arrays to vectors and it will work.
*) Well, almost, but don't worry about that. It works for ints too.
It is trivial to express in code. If you can't translate my description into code, you're not trying hard enough.
So why is your original code using operator new? operator new performs dynamic memory allocation, with a length determined at RUN TIME.
While it is true I told the OP off for not trying, I also gave complete advice. Deliberately.
There is the problem jeedi khan would rather cry to be spoon-fed with code, than TRY to produce code from a description. What s/he hasn't realised is that the effort for him/her to produce the code from the description is less than the effort s/he has already expended trying to get somebody to do that task.
Because Adak pretty much did that over in the C forum for him.There is the problem jeedi khan would rather cry to be spoon-fed with code, than TRY to produce code from a description.
I did not intend to try and start a flamewar. My contribution was that in my experience some learning/teaching institutions actually do use C++ as their "language" and then go about teaching things the "C way". This wasn't aimed at the OP either... I'm sorry if it sounded that way. I did use his source code as an example of "context" after the OP asked, but that was only for convenience (the approach being taken is more "c like" than "c++ like"). So, in summary, I apologise to the OP and the contributors to this thread; my comments were an observation only and not directly related to the OP or this thread.
As far as i took the purpose of this foum is to give a direction to the existing work one done on a program ....if one is trying and he is not getting so the right direction to him be sought...Adak did that for me and thats the purpose but if you/someone else are not willing to do so ,so Kindly don't indulge in unnecessary details about others.This is the Problem i observed here with SirPrattlepod too..ONE SHOULD NOT beat about the bush if s/he is not willing to guide others..Infact i tries myself first and again and again but when unable to do so then post here...
Ok. Penance. I don't really know what it does though.
[/quote]Code:// crc code.cpp : Defines the entry point for the console application. // #include <iostream> //#include <conio.h> // OS-Specific include. Removed. //#include <malloc.h> // Not needed for new/delete (they are operators) //#include"stdafx.h" //#define size len; // Note the semi-colon... it should not be here // The macro isn't used anyway, so commented it out #include <stdio.h> using namespace std; // Formatted using astyle -A4 -U -p -f filename.cpp int main(void) { int message[10], i, lim_fcs, n, tot_len; int pattern[50], msg_len, patt_len, len; cout << "Enter the number of bits for message length(length should be " "within 10 bits):" << endl; cin >> n; cout << "Enter the message to send:" << endl; /* Should add error checking here/above to ensure that n is <= 10 * or better still have 10 as a #define or const, or check that n is * <= sizeof message / sizeof message[0] */ for(i = 0; i < n; i++) { cin >> message[i]; } cout << "The message to send is:" << endl; for(i = 0; i < n; i++) { cout << message[i]; cout << "\t"; } cout << "\n"; msg_len = n; cout << "Enter the limit for FCS:" << endl; cin >> lim_fcs; // The message below is wrong... it should be within 50 cout << "Enter the number of bits for Pattern(limit to be " "within 10 bits)" << endl; // pattern bits taken here cin >> patt_len; /* Should add error checking here/above to ensure that patt_len is <= 50 * or better still have 50 as a #define or const, or check that patt_len is * <= sizeof pattern / sizeof pattern[0] */ cout << "Enter the pattern bits:" << endl; for(i = 0; i < patt_len; i++) { cin >> pattern[i]; } cout << "The pattern is:\n"; for(i = 0; i < patt_len; i++) { cout << pattern[i]; cout << "\t"; } cout << endl; // Since you used '\n' above maybe replace this with '\n' // or change the prior usage to cout << endl; Consistency // is good tot_len = msg_len + lim_fcs; len = tot_len - patt_len; int *a = new int[len](); // Array of zeroes cout << "The array is:\n"; for(int j = 0; j < len; j++) { cout << a[j]; cout << "\t"; } cout << endl; int newsize = len + patt_len; // NEED here is an array patternnew[newsize] having elements of pattern[] // followed by zeros of a[].... int *foo = new int[newsize]; int pos = 0; for (int j = 0; j < patt_len; j++, pos++) { foo[pos] = pattern[j]; } for (int j = 0; j < len; j++, pos++) { foo[pos] = a[j]; } cout << "foo is:\n"; for(i = 0; i < newsize; i++) { cout << foo[i]; cout << "\t"; } cout << endl; delete [] a; delete [] foo; getchar(); return 0; }
This is called spoon-feeding, and it is not a good thing. You learn so much more by finding the answer yourself than getting it handed to you.
Furthermore, this code is garbage, and if I ever saw something like that in my code, I'd throw it out without second thought.
Big dislike.
Vectors rule, and that is all I have to say on the matter. If the OP cannot be bothered to even experiment with them, then he/she has a sad future ahead. Not as a programmer.
Did I say it was good? Did I say it was a good way to do it (I already said it was CRAP and got accused of trying to start a flamewar). Furthermore, it's not my code I just added a couple of lines and some comments regarding the OPs code. So, we have come full circle and you reveal your true colours -- I was correct in the first place.
but the point is that you gave the OP a full answer to the problem at hand. this forum has very clear rules about this.
the accuracy of this statement is highly dubious. Elysia can be pretty tough on people sometimes, but she does it for their benefit. she knows what she's talking about, and most importantly, she understands the forum rules. I suggest you learn them too.
you're a very new user here, so my recommendation is that you watch and pay attention to how the regulars conduct themselves here, and try to follow that example.
What can this strange device be?
When I touch it, it gives forth a sound
It's got wires that vibrate and give music
What can this thing be that I found?
I disagree strongly. The thread was going nowhere. I could have given a response in pseudocode I guess, or given subtle hints, or other crazy stuff, but at the end of the day writing it in code is equivalent. In my defence my "answer" is the same as if I'd given an English description, a mathematical description, a pseudocode description, or whatever.
It's not inaccurate. They (Elysia) attacked me by saying my previous answers were non-constructive. They implied that I was saying the OPs code was crap (which I never did). I then added, what... 8?, lines plus a few comments to the OPs source code and then Elysia insulted the OP by saying their source code was complete and utter rubbish? Something doesn't add up here. I don't care if Elysia has 1 post or a million posts I'll be honest every time, even if the honesty seems brutal. Live with it.the accuracy of this statement is highly dubious. Elysia can be pretty tough on people sometimes, but she does it for their benefit. she knows what she's talking about, and most importantly, she understands the forum rules. I suggest you learn them too.
To be honest I'm not overly impressed with "the regulars" and their conduct. Should I quiver in fear and follow like a lost lamb? Nah. Of course not. How new I am or how "old" they are is not relevant. If I think their example is wrong I'll say so and not just bow to their experience... that'd be a bit silly.you're a very new user here, so my recommendation is that you watch and pay attention to how the regulars conduct themselves here, and try to follow that example.
Edit: I do realise your post was written with the best of intentions and I thank you for that. But, please, do not suggest to me how I should conduct myself.
Last edited by SirPrattlepod; 08-21-2013 at 07:00 AM.