Answer» Hi i've started learning to program C++ some weeks ago. I have this pdf book that i learn from but i have run into some troubble with a program. The program is as stated here:
#include using namespace std;
void cube(int *n, num);
int main() { int i, nums[10];
for(i = 0; i < 10; i++) nums = i + 1;
cout << "Original contents: "; for(i = 0; i < 10; i++) cout << nums << ' '; cout << '\n';
cube(nums, 10);
cout << "Altered contents: "; for(i = 0; i < 10; i++) cout << nums << ' ';
return 0; }
My book tells me to write that. It should show something about array passing from one function to another but i simply cant get it to do as its supposed to. I use borland c++ compiler 5.5 to compile with.
please help me. Sincerly Vikkenhi..vikken.! i'm just wondering if that code is correct or n0t.. the 'nums[10]' variable was DECLARED as an array right? now, in your code inside the for loop, you assign a value to the variable 'nums' which is n0t an array, coz' it does n0t have []..why n0t try to make it nums[ i ]=i+1;
..hope it helps.!actually, it does include the indexing square brackets, but since it was directly in the post, the forum interpreted the [ i ] (without spaces) as an italics TAG.
here is the full code, in a helpful code tag
Code: [Select]#include <iostream> using namespace std;
void cube(int *n, num);
int main() { int i, nums[10]; for(i = 0; i < 10; i++) nums[i] = i + 1; cout << "Original contents: "; for(i = 0; i < 10; i++) cout << nums[i] << ' '; cout << '\n'; cube(nums, 10); cout << "Altered contents: "; for(i = 0; i < 10; i++) cout << nums[i] << ' '; return 0; }
Since I'm here, I may as well offer some relevant help;
The first thing that jumps out is that although you declare the "cube" function, you haven't provided a DEFINITION for it; therefore my attempts to compile as-is give me unresolved externals.
However, I added this definition in, so I could proceed to test:
Code: [Select]void cube(int *n, int num) {
for(int i=0;i<=10;i++) n[i]=n[i]*num;
}
Pretty simple; it simply multiplies each element of the 11 element array (as you declared it; note that declaring a variable like variable[n] will create a array with n as the highest index; therefore you get n+1 elements, since 0 is the lower bound.)
I get the following output, which confirms that it works:
Code: [Select]Original contents: 1 2 3 4 5 6 7 8 9 10 11 Altered contents: 10 20 30 40 50 60 70 80 90 100 110 (I also changed the for loops from " for(i = 0; i < 10; i++)" to " for(i = 0; i <= 10; i++)")
It definitely seems that it works just fine.
truthfully, the square brackets are actually memory indexing operators; you can use them on any type in C, but C++ restricts their use and you get warnings when you do freaky stuff sometimes, but largely, it's possible to use it for nearly anything. Any pointer type, for example, can be indexed into.
with a pointer type (in the "cube" prototype, the first parameter is being passed as a pointer to a int) when you use the array/memory indexing operator (the square brackets) it returns the value in memory at the pointers position plus the array index times the array size. For example, lets say you have a char array "chars" and it contains the string "characters". with a pointer, you can do something like this: Code: [Select]char *charsp = &chars[0]; singchar = charsp[4]; It looks pretty straightforward, but remember, charsp is not an array type! so how does it work? As I said, it's indexing directly into memory at that location based on the passed index. with your standard char, this means it is accessing the 4th byte after the byte pointed to by the char itself (in this case the first one). However, if instead of char types we were dealing with, say, wide characters, then each char would be two bytes, so the indexing would do the math of 4*2, so it would grab the two byte character at position 10 (we add two because the first element is also two bytes).
I hope this helps clear things up; you weren't specific as tou what kind of trouble you were running into, and the code you provided works fine for me (Visual Studio 2008, btw) So I thought I'd see if I could help clarify the array/pointer business.BCP, I see you cleverly understood what a "cube" function should return... it's always good to help people get top marks for their homework.
Quote from: Salmon Trout on November 14, 2010, 10:24:19 AM BCP, I see you cleverly understood what a "cube" function should return... it's always good to help people get top marks for their homework.
There is no possible definition for a "cube" function that would accept two parameters that I'm aware of. My first thought was of course to simply make a cube function, but clearly the prototype was important. I had to guess what the function did from the context, and the parameters make the obvious choice of f(x)=x*x*x infeasible. My second guess would be a cube function that for some reason cubes only the nth parameter, but it would be dumb to only cube the 10th parameter and leave everything else untouched, and I certainly hope a book isn't advocating such nonsensical parameter conventions like creating an otherwise bog-standard cube function but making it accept an entire array for the purpose of cubing only a single element.
And since no sample output was provided, I just made a standard function that simply used it's parameters in a meaningful way. To my UNDERSTANDING the entire purpose of the exercise was to deal with arrays; and I would certainly hope they would use their definition of the cube() function rather then mine; being that mine was only created due to the lack of the latter and the inability to compile and test without a meaningful definition.
Thank you very much it was very helpful Quote from: Vikken on November 14, 2010, 12:34:39 PMThank you very much it was very helpful
You're welcome
You might want to double-check my information against a trustworthy source; I did no research for it and it's from my memory so chances are I made a few errors in my recollection of the semantics.Quote from: BC_Programmer on November 14, 2010, 09:14:42 AMactually, it does include the indexing square brackets, but since it was directly in the post, the forum interpreted the [ i ] (without spaces) as an italics tag. ..oh.! plz excuse me sir, for im new here(actualy that was my first post)...n0w i kn0w..thanks.!
@TS you may define the cube function first..for what u have s just a prototype..
|