Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
print the sum in a new array

What I have tried:

yes i had tried not able to rectify
Posted
Updated 26-Sep-21 14:20pm
Comments
Patrice T 25-Sep-21 13:26pm    
Show your work and explain your problem.
Tushar Shukla 2021 25-Sep-21 13:49pm    
i have uploaded my solution can you pls take a look over it

Quote:
yes i had tried not able to rectify

If your problem is that you get wrong result, I would try:
C++
int n;
cin>>n;
int arr[n],arr1[n];
int sum = 0;
for( int i =0;i < n; i++){
  cin>>arr[i];
}
int j=0;
for(int i = 0;i < n; i++){
  sum = sum + arr[i];
  arr1[j] = sum;
   j++;
}
cout<< arr[j];
cout<< arr1[j-1];  // because j was incremented after storing last sum in array

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v2
Comments
merano99 26-Sep-21 19:59pm    
Shouldn't the result be in arr1?
Patrice T 27-Sep-21 1:28am    
Sure :)
CPallini 27-Sep-21 2:07am    
5.
Patrice T 27-Sep-21 2:09am    
Thank you.
Based on the Code Patrice T has posted, here an other version:

C++
int n;
cout << "Enter the number of values in the array: ";
cin >> n;

vector <int> arr(n), arr1(n);

for (int i=0; i<n; i++) 
	cin >> arr[i];

arr1[0] = arr[0];
for (int i=1; i<n; i++)
	arr1[i] = arr1[i-1] + arr[i];

cout << arr1[n-1] << "\n";  // print last sum in array
 
Share this answer
 
Comments
Patrice T 27-Sep-21 1:29am    
+5
CPallini 27-Sep-21 2:08am    
5.
merano99 27-Sep-21 18:23pm    
Thank you.
What about learning the language in some Learn C++ tutorial and coding the solution and understanding the code.

tip: be sure that you understand the code of your homework, because your tutor may ask you to explain it
 
Share this answer
 
Comments
Tushar Shukla 2021 25-Sep-21 13:56pm    
can you pls tell me where i m lacking in this code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900