Array in C++ - Beginners Help

Hi,

I'm having some doubts about sum in arrays.

--> As far as I know, to sum values of collumns the array has to be an INT:
int v_array[0][9];
In this example the array has just one line and 10 collumns.


--> Here I assigned values to the collumns:
puts("Insert the number:");
scanf("%d",v_array);
// Here the program get lost...

--> Then,I have to take an especific value and sum 1:
v_sum = (v_array[0][2]) + 1;

--> The result:
printf("result: %d",v_sum);

-----------------------------------------------------------------------------------
Complete code:

#include <stdio.h>

int main (){
int v_array[0][9];
int v_sum;

puts("Insert the number:");
scanf("%d",v_array);

v_sum = (v_array[0][2]) + 1;
printf("result: %d",v_sum);

return(0);
}
--------------------------------------------------------
Another example that does not work, using strings:
int main (){

char v_string[1][9];
int v_sum;
puts("Insert the number:");
scanf("%s",v_string);

printf("The number inserted is: %s\n", v_string);

printf("Position 2: %c \n\n",v_string[0][2]);

v_sum = v_string[0][2]+1;

printf("result 0: %d",v_sum);
}
-----------------
Sorry for the bad english. :)
Hey, please use code tags for your code - http://www.cplusplus.com/articles/jEywvCM9/

As far as I know, to sum values of collumns the array has to be an INT:

They can be of types like double and float too, for decimals.

int v_array[0][9];
In this example the array has just one line and 10 collumns.


Yes, array indexes starts at 0. But when you create them it doesnt work that way.

What you want is int v_array[1][10] // 1 row 10 columns. Access via v_array[0][0-9]

You've done it correctly here so not sure why you didnt do it the same way with the other array char v_string[1][9];
Last edited on
Thanks!

I will correct my code.
Hi,

When using any of the scanf functions, make use of it's return value to see if it worked. Asking for trouble otherwise :+)

The same applies to fprintf or their variants, but probably pedantic to apply to printf itself :+)

Regards
Topic archived. No new replies allowed.