Getting wrong output for my program in C++ - c++

#include <iostream>
using namespace std;
int num(int n){
for(int i= 1 ; i<=n ; i++){
int sum=0;
sum += i;
cout<<sum;
}
}
i guess this part is clear.
int main()
{
int x;
cout<<"enter the value of x ";
cin>>x;
int answer=num(x);
cout<<"the total sum of the first n integer is "<<answer;
return 0;
}
ive tried looking it up but no results found....
ive always had some trouble with the loops.

You are throwing away what is calculated by previous iterations.
You are not returning what is calculated.
To fix, get the declaration and initialization of i out of the loop and return the result.
#include <iostream>
using namespace std;
int num(int n){
int sum=0;
for(int i= 1 ; i<=n ; i++){
sum += i;
cout<<sum;
}
return sum;
}

Try moving the definition and initialization of the variable sum to outside of the for loop, preferably before it. Do not forget your return value from the function.

The function num() should return the variable sum and variable Sum should be defined outside for loop.
Your code should be
#include <iostream>
using namespace std;
int num(int n){
int sum=0; //sum should be declared outside for loop
for(int i= 1 ; i<=n ; i++){
sum += i;
// cout<<sum; //dont print sum here it will be printed in main()
}
return sum; //you missed this
}
int main()
{
int x;
cout<<"enter the value of x ";
cin>>x;
int answer=num(x);
cout<<"the total sum of the first n integer is "<<answer;
return 0;
}

Related

Partition of Array by Element given X

I Am Trying To Find Partition Of Array ,On Condition By Checking Variable x ,when less then x they will be on one side or else on another. but my code need some correction.
HERE am not able to find the error , i will be thankful to you if you help me.
Code is:-
#include<iostream>
using namespace std;
int partition(int arr[],int n,int x){
for(int i=0;i<n;){
if(arr[i]<x){
i++;
}
else if(arr[i]==x){
int temp=arr[i];
arr[i]=arr[n];
arr[n]=temp;
i--;
}
else if(arr[i]>x){
int temp=arr[i];
for(int j=i;j<n;j++){
arr[j]=arr[j+1];
}
arr[n]=temp;
i--;
}
}
return 0;
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int x;
cin>>x;
partition(arr,n,x);
for(int i=0;i<n;i++){
cout<<arr[i]<<"\t";
}
return 0;
}
Input >> array={2,10,15,1,3,15} ,x=10
Expected << {2,1,3,10,15,15}
Output I get << nothing .
The code isn't giving any output because, first, the "cin" and "cout" are in upper case which is syntactically incorrect, secondly, the variable j is in different case in loop statement and body inside the second else-if clause in the partition function, same goes for the "I" in the first for loop in the main() function. Sort this out and you should be good to go.
First in C++ the size of an array must be a compile-time constant. So for example, consider the following examples:
int n = 10;
int arr[n]; //INCORRECT
The correct way to write the above would be:
const int n = 10;
int arr[n]; //CORRECT
Similarly, in your code,
int n;
cin>>n;
int arr[n]; //INCORRECT because n is not a constant expression
Second, in your code, when you wrote:
arr[n] = temp; Undefined behavior
you're going out of bounds and so you have undefined behavior.
Solution
You can use std::stable_partition and std::vector to solve your problem as shown below:
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
int n;
std::cout <<"Enter n:"<<std::endl;
std::cin >> n;
std::vector<int> arr(n); //create a vector of size n instead of an array
std::cout<<"Enter elements: "<<std::endl;
//iterate and take input from user
for(int &elem: arr){
std::cin >> elem ;
}
int x;
std::cout << "Enter x: "<<std::endl;
std::cin>>x;
//use std::partition
std::stable_partition(arr.begin(), arr.end(), [x](int i){return (i < x);});
std::cout<<"This is the partitioned vector: "<<std::endl;
for(int i=0;i<n;i++)
{
std::cout<<arr[i]<<"\t";
}
return 0;
}
Output
The output of the above program is as follows:
Enter n:
6
Enter elements:
2
10
15
1
3
15
Enter x:
10
This is the partitioned vector:
2 1 3 10 15 15
which can be seen here.

Why am I getting the wrong output for this C++ code? (one of the problem of hackerrank)

This is the program for printing out sum of array elements. It is showing run time error. The output is coming out to be 0 instead of printing out the sum of the elements.
#include<iostream.h>
using namespace std;
void simpleArraySum()
{
int ar[100],n,i,sum=0;
for(i=0;i<n;i++)
{
sum=sum + ar[i];
}
cout<<sum;
}
int main()
{
int ar[100],n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
simpleArraySum();
return 0;
}
On this line in your main:
int ar[100], n;
You create an array of 100 elements. You later fill that array using cin
for(int i = 0 ; i < n ; i++)
{
cin >> ar[i];
}
Then you do nothing with that array. You are not calculating any sum. You let that array go, forgotten.
Then, you call a simpleArraySum function. That function is creating an entirely new, distinct array.
// v-----v------There
int ar[100],n,i,sum=0;
That array has no value assigned to it. In fact, reading from it is undefined behavior.
What you want is to receive that array in the arguments of your function:
void simpleArraySum(int* ar, int n) {
// ...
}
And call it like that in your main:
simpleArraySum(ar, 100);
You can avoid the issues of arrays and functions by not using them:
int main()
{
int quantity = 0;
std::cin >> quantity;
int sum = 0;
int value;
while (std::cin >> value)
{
sum += value;
}
std::cout << sum << "\n";
return EXIT_SUCCESS;
}
In simpleArraySum, the variable n is uninitialized. So this loop:
for(i=0;i<n;i++)
invokes undefined behavior when reading from n.
Also, you are summing a different array in the function, than the one you read in mian. It seems that you need to pass in the array from main to this function:
void simpleArraySum(int *ar, int n) {
and call it like this:
simpleArraySum(ar, n);
Finally, you don't even need a function for this, since there is an existing algorithm std::accumulate that you can use:
cout << std::accumulate(ar, ar + n, 0);
In the function, you're adding the elements of ar which is local to the function simpleArraySum() and is not of the array ar that is local to main().
So, pass the array and its length to the function and return its sum. Here is your corrected code:
#include<iostream>
using namespace std;
void simpleArraySum(int ar[], int n)
{
int i, sum = 0;
for(i=0;i<n;i++)
{
sum=sum + ar[i];
}
cout<<sum;
}
int main()
{
int ar[100],n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
simpleArraySum(ar, n);
return 0;
}

Why am I not getting any output(linear search)?

#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
I was trying to do linear search program but there was no output, what is wrong in the code?
I think the problem is that you declared " i " twice and that has no sense. Here is the right code:
#include<iostream>
using namespace std;
int main(){
int n, i=0;
int current_element;
cin>>n>>current_element;
int arr[n];
for(i=0;i<n;i++){
cin>>arr[i];
}
for(i=n-1;i<=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}
This way you initialize " i " once with the value of 0 and it should work.
i think its because in your for loop you said
for(int i = n-1; i <= 0; --i)
this doesnt seem like what you are trying to do as it says that it will keep running as long as i<=0 however i is never less than or equal to zero. i think you would want to write i>=0 instead. also, you have 2 "i" variables which you initialized.
It seems that you made a mistake on the second loop clause.
Your code
...
//loop condition is causing the problem
for(i=n-1;i<=0;--i){
...
Fixed code
...
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
...
The loop condition causes the program to not enter the second for loop. Modifying it makes the program work.
Full code
#include<iostream>
using namespace std;
int main(){
int n;
int current_element;
cin>>n>>current_element;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int i;
//loop condition is causing the problem
for(i=n-1;i>=0;--i){
if(current_element==arr[i]){
cout<<i;
}
else{
cout<<"-1";
}
}
}

Return value always 0 unless I use cout

The function should return 156 with a "Z" input but it outputs 0.
If the letter's U-Z, the function finds the largest factor of the letters numeric value other than the value itself and multiplies it by 12.
Tried putting cout in the function and it works somehow. Don't know where's the problem. Someone help.
#include<iostream>
#include<string>
#include<ctype.h>
using namespace std;
int uz(int n);
int main(){
string letter, alphabet="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int alphSize=alphabet.size(), l=0, total=0;
cin>>letter;
int letterSize=letter.size();
for(int i=0;i<letterSize;i++){
for(int j=0;j<alphSize;j++){
if(toupper(letter[i])==alphabet[j])
l=j+1;
}
if(l>20&&l<=26){
l=uz(l);}
total+=l;
}
cout<<total<<endl;
return 0;
}
int uz(int n){
int size=1;
int arr[size];
for(int i=1;i<=n;i++){
if(n%i==0){
size++;
arr[size]+=i;
}
}
n=12*arr[size-1];
//cout<<n;
return n;
}

fibonacci sequence sum total error

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int last;
do{
system("cls");
cout<<" FIBONACCI "<<endl;
int a,sum;
cout<<"Enter the number of outputs you want to be displayed : ";
cin>>a;
long long unsigned int b= 0, c=1;
while(a >=0)
{
cout<<b<<endl;
sum+=b;
b=b+c;
c=b-c;
a--;
}
cout<<"total = "<<sum<<endl;
cin>>last;
}
while(last==0);
system("pause");
return 0;
}
whenever i want to repeat it by giving 0 as the last input the value of sum doesn't reset itself and the new sum gets added to the previous one and a wrong value is displayed as the total.
your variable sum is not initialized before you access it.
You should change
int a,sum;
to
int a;
int sum = 0;
You have declared sum incorrectly it should be the same type as b and c
long long unsigned int sum;
and sum was uninitialised too, but: your calculation is very strange
sum+=b;
b=b+c;
c=b-c;
So I recommend
sum = c + b;
b = c;
c = sum;
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int last;
do{
system("cls");
cout<<"FIBONACCI \n\n";
int a,sum;
cout<<"Enter the number of outputs you want to be displayed : ";
cin>>a;
long long unsigned int sum=0,b=0,c=1,d=0;
while(a >=0)
{
cout<<b<<endl;
sum+=b;
b=d+c;
c=d;
d=b;
a--;
}
cout<<"total = "<<sum<<endl;
cin>>last;
}
while(last==0);
system("pause");
return 0;
}
the change was made in calculation and sum is initialised to zero in beg of the do..while loop and sorry for bad punctuation