0

Hi,I cannot understand ,why the sum is incorrect ? please answer me.

#include <stdio.h> int main(){ int a,b,t[7],n,i,d[7],s; scanf("%d",&n); s=0; for(i=0;i<n;i++){ scanf("%d",&t[i]); printf("t[%d]=%d\n",i,t[i]); d[i]=t[i]; printf("d[%d]=%d\n",i,d[i]); s=s+d[i]; } printf("s:%d",s); return 0; } https://code.sololearn.com/cBzQC9nz3Zr4/?ref=app https://code.sololearn.com/WyrqKKv949KT/?ref=app

11th Dec 2020, 5:25 PM
Rami Joy
Rami Joy - avatar
6 Respuestas
+ 2
You don't have any compiler error in your code. Please give some examples on.. - How you want your code to work and - What is going wrong. Also don't attach others code which are irrelevant to your question.
12th Dec 2020, 2:37 AM
Minho
Minho - avatar
+ 1
You want to do something like this? https://code.sololearn.com/ci1U4yJjlQlp/?ref=app Btw it's called an array not vector. C doesn't have vector but you can create your own vector type by using the typedef keyword.
12th Dec 2020, 6:02 AM
Minho
Minho - avatar
+ 1
Arrays are a special type of variable that can store multiple data values of the same type. (Primitive types only, like int or float) In C, you have to declare an array with a specific size before you can use it. The array will then be able to store data, according to its size. You cannot store more data than the initial array size you specified. But a vector is a type of array you will find in Object Oriented languages. Like arrays, they can store multiple data values but they cannot store primitive data types. They only store object references. They point to the objects that contain the data instead of storing the objects themselves. Also, you don’t have to declare the size of a vector. It will grow, or shrink, as you fill it with object references or remove them. C is not an object oriented language. So I guess you don't know what an object is yet? If you want to learn more about object oriented programming then you might take a look into c++. https://www.sololearn.com/learn/261/?ref=app
12th Dec 2020, 8:46 AM
Minho
Minho - avatar
0
I want it to calculate the true sum of all vector's elements.
12th Dec 2020, 4:18 AM
Rami Joy
Rami Joy - avatar
0
Thank you
12th Dec 2020, 8:16 AM
Rami Joy
Rami Joy - avatar
0
But can I know what's the diffrence between array and vector in C language ?
12th Dec 2020, 8:27 AM
Rami Joy
Rami Joy - avatar