What did I do wrong? C++ Newbie here [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I thought the output would be 70? (20+20+20+10=70) Why is it so large?
#include <iostream>
using namespace std;
int main()
{
int a,b,c=20;
int d=10;
int sum = a+b+c+d;
cout << sum;
return 0;
}

The issue is that you are not initializing the variables a and b. That means when you attempt to run your program, the computer is looking in memory for a value to use for each, and that number could be very big or very small. Try this:
#include <iostream>
using namespace std;
int main()
{
int a = 20,b = 20,c=20; //here, a and b are defined
int d=10;
int sum = a+b+c+d;
cout << sum;
return 0;
}

C is the only one variable that you initialize to 20, the other 2 variables
(a and b) are holding garbage..
so your math calculation is undefined behaviour.

Related

A puzzle game from codechef [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Could somebody explain the solution to this problem in cpp and how it works
Can this be made more efficient using STL?
http://www.codechef.com/problems/H1
It would very helpful if you could explain this particular logic along with its time complexity.
#include <cstdio>
using namespace std;
int board[9],q[178000]={123456789},powers[9]={100000000,10000000,1000000,100000,10000,1000,100,10,1};
int posn,fromposn,front=-1,back=1;
char found[98765435]={0};
bool prime[18]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1};
void moveit(int p1,int p2){
int diff=board[p2]-board[p1];
posn=fromposn+diff*powers[p1]-diff*powers[p2];
int posndiv10=posn/10;
if(!found[posndiv10]){
found[posndiv10]=found[fromposn/10]+1;
q[back++]=posn;
}
}
int main(){
int t,i,d;
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
found[12345678]=1;
while(++front<back){
for(posn=fromposn=q[front],i=8;i>=0;i--){
board[i]=posn%10;
posn/=10;
}
if(prime[board[0]+board[1]]) moveit(0,1);
if(prime[board[0]+board[3]]) moveit(0,3);
if(prime[board[1]+board[2]]) moveit(1,2);
if(prime[board[1]+board[4]]) moveit(1,4);
if(prime[board[2]+board[5]]) moveit(2,5);
if(prime[board[3]+board[4]]) moveit(3,4);
if(prime[board[3]+board[6]]) moveit(3,6);
if(prime[board[4]+board[5]]) moveit(4,5);
if(prime[board[4]+board[7]]) moveit(4,7);
if(prime[board[5]+board[8]]) moveit(5,8);
if(prime[board[6]+board[7]]) moveit(6,7);
if(prime[board[7]+board[8]]) moveit(7,8);
}
scanf("%d",&t);
while(t--){
for(posn=i=0;i<8;i++){
scanf("%d",&d);
posn=posn*10+d;
}
scanf("%d",&d);
if(found[posn]) printf("%d\n",found[posn]-1);
else puts("-1");
}
return 0;
}
It first builds a table of all the solvable boards, represented as numbers, that says how many steps from the solution that board is.
Then it solves each test case by looking it up in that table.
The time complexity per test case is constant.
I doubt that there is anything in the standard library that could make it more efficient.

program to view addresses of elements in an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
help plz its showing invalid indirection
i used it to find the location or memory addresses of elements in the array b
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *ptr;
int b[]={1,0,2,3,4,5,6,7,8,9};
ptr=b;
for(int i=0;i<10;i++)
cout<<ptr[i]<<" "<<*b[i];
}
In order to print the address of the ith element in an array b, use
std::cout << b + i;
This will work in all cases except when b is an array of char, in which case you need to cast to void*
std::cout << static_cast<const void*>(b + i);
in place of iostream.h it should be iostream.
void main() ; it should be int main().
cout<<ptr[i]<<" "<<(b+i)<<endl;
Formatting by using endl in above line of code will make the result clear.
Your function should return an integer value.
return 0;

C++'s min_element() not working for array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to find the minimal element of a subset of an array in c++ as follows:
#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
using std::min_element;
void load_values(int n);
unsigned *w;
int main() {
int n, a, b;
cin >> n >> a >> b;
w = new unsigned[n];
load_values(n); // sets values to w[i]
int min = min_element(w+a, w+b);
}
void load_values(int n) {
while(n--)
w[n] = 1;
}
I get the error invalid conversion from 'unsigned int*' to 'unsigned int' [-fpermissive]. What am I missing here?
Note, that std::min_element() returns an iterator to a minimal value, not the value itself. I doubt that you get this error based on the above code alone: assuming proper include directives and using directives the code compiles OK although it would print the address of the minimum element rather than its value. Most likely your actual code looks more like
unsigned min = std::min_element(w + a, w + b);
Note, that you should also check the result of reading from std::cin:
if (std::cin >> n >> a >> b) {
// process the values
}
... and, of course, it seems you're leaking the memory allocated for w.

Is there a efficient way to do multiple test cases in c++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I use MS Visual Studio and I am new to C++, so I am just wondering if there is an faster more efficient way to do multiple test cases instead of keep clicking CTRL+F5 and re-opening the console many times.
Like for example if I have this code
#include <iostream>
using namespace std;
void main ()
{
int x;
cout<<"Enter a number"<<endl;
cin>>x;
cout<<x*2<<endl;
}
Is there a way I could try different values of x at once and getting the results together?
Thanks
Simple work around:
while(terminating_condition_is_not_met)
{
execute_what_you_want
}
terminating condition can be EOF or max_no_of_iterations or some_sentinel_value
And for your code, I used -1 as a sentinel.
#include <iostream>
using namespace std;
void main ()
{
int x;
while(1)
{
cout<<"Enter a number"<<endl;
cin>>x;
if(x==-1)
break;
cout<<x*2<<endl;
}
}

Why won't my code compile? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
This is a code that takes a series of 3 numbers in a number pattern and figures out the difference between them. everything seems to be right but my compiler keeps telling me I need an initializer before int i? sorry, I'm new to C++ so I'm sure my code is horrible.
using namespace std;
void add(int a, int b, int c)
int i;
for (a+i!=b;b+i!=c)
{i=0; i<100; i++;}
else {cout i;}
};
int main()
{
int x, y, z;
cin>>x;
cin>>y;
cin>>z;
add(x, y, z);
}
Many things, first you're missing a curly braze after your add function.
Also, you have one extra ; in your for declaration.
Also, after your function add there shouldn't be a ;