In python, I would use a list to store some numbers than print all of them:x = [1, 2, 3, 4]
print(x) #output = [1, 2, 3, 4]
How should I do it in C with an array?
Another question: is an array similar to the python lists? If not, what type would be it?;
What you are doing is printing the value in the array at spot [3][3], which is invalid for a 3by3 array, you need to loop over all the spots and print them.
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", array[i][j]);
}
printf("\n");
}
This will print it in the following format
10 23 42
1 654 0
40652 22 0
if you want more exact formatting you'll have to change how the printf is formatted.
There is no .length property in C. The .length property can only be applied to arrays in object oriented programming (OOP) languages. The .length property is inherited from the object class; the class all other classes & objects inherit from in an OOP language. Also, one would use .length-1 to return the number of the last index in an array; using just the .length will return the total length of the array.
I would suggest something like this:
int index;
int jdex;
for( index = 0; index < (sizeof( my_array ) / sizeof( my_array[0] )); index++){
for( jdex = 0; jdex < (sizeof( my_array ) / sizeof( my_array[0] )); jdex++){
printf( "%d", my_array[index][jdex] );
printf( "\n" );
}
}
The line (sizeof( my_array ) / sizeof( my_array[0] )) will give you the size of the array in question. The sizeof property will return the length in bytes, so one must divide the total size of the array in bytes by how many bytes make up each element, each element takes up 4 bytes because each element is of type int, respectively. The array is of total size 16 bytes and each element is of 4 bytes so 16/4 yields 4 the total number of elements in your array because indexing starts at 0 and not 1.
Print an array of objects in c - Stack Overflow
Print Out an Array of Objects C++ - Stack Overflow
How do I print array of objects? Objective C - Stack Overflow
C: print an array - Stack Overflow
Videos
In your class, you need to provide a method for the description selector. This will return an NSString* of the contents of your class, formatted as you wish.
For example:
-(NSString*)description
{
NSString* str1 = [NSString stringWithFormat:@"Purchase Share Price = %f",currentSharePrice];
NSString* str2 = [NSString stringWithFormat:@"Current Share Price = %f",currentSharePrice];
... // do the rest of the items
NSArray* strings =[NSArray arrayWithObjects:str1,str2,<the rest of them>, nil]
NSString* result = [strings componentsJoinedByString:@"\n"];
return result;
}
And then:
NSLog("%@",bolsa);
NOTE: This is a good approach whenever you need to debug/log objects in objective-c...having a method to convert complex objects into simple representations (i.e. a string) can be really helpful. Coding skill is not just knowing about functions and templates...its also about techniques and tools.
In your StockHolding class implement that method:
- (NSString*) description
Than use:
NSLog(@"%@", bolsa);
It will iterate through array and print string taken from above method for each object inside this array.
That is the memory address of the array (because of array-to-pointer decaying).
To correctly print pointers though, you should use %p.
The "wierd" value you get from printing array is actually the memory address of the first element of the array.
In order to print an array, you need to use a for loop:
for (int i = 0; i < array_size; i++)
printf("%d\n", array[i]);
A C function for printing the entire array, feel free to use it!
void printArray(int length, int array[])
{
for (int i = 0, n = length; i < n; i++)
{
if (i == n - 1)
{
printf("%i.", array[i]);
}
else
{
printf("%i, ", array[i]);
}
}
printf("\n");
}