I was doing the hackerrank triplet sum question and I found these errors but I dont know why these errors are coming. Can someone please give a little more insight about it and if possible link a video about the topics I should read in order to atleast get the code correct.
The code
#include <iostream>
using namespace std;
int compareTrip(int a[],int b[])
{
int i=0,result1=0,result2=0;
for(i=0;i<3;i++)
{
if(a[i]>b[i])
result1++;
if(a[i]<b[i])
result2++;
else {
return 0;
}
}
}
int main()
{
int i,a[3],b[3];
for(i=0;i<3;i++)
{
cin>>a[i];
}
for(i=0;i<3;i++)
{
cin>>b[i];
}
compareTrip(a[], b[]);
}
The Errors
Solution.cpp:30:19: error: expected primary-expression before ‘]’ token
compareTrip(a[], b[]);
^
Solution.cpp:30:24: error: expected primary-expression before ‘]’ token
compareTrip(a[], b[]);
^
Solution.cpp: In function ‘int compareTrip(int*, int*)’:
Solution.cpp:18:1: error: control reaches end of non-void function [-Werror=return-type]
}
^
static List<Integer> compareTriplets(List<Integer> a, List<Integer> b) {
List<Integer> points=new ArrayList<Integer>(2); ;
int apoint=0,bpoint=0;
for(int i=0;i<3;i++)
{
if(a.get(i)>b.get(i))
{
apoint++;
}
else if(a.get(i)<b.get(i))
{
bpoint++;
}
else
{
}
}
points.add(0,apoint);
points.add(1,bpoint);
return points;
}
These first two errors occur because the compiler thinks you're trying to redefine the arrays a and b, which you've already initialized the at the beginning of main. When you call compareTrip() , you just need to pass in the names of those arrays as the two arguments.
The error control reaches end of non-void function indicates that there is no return value at the end of the function at line 18. So you have a set of if/else statements in compareTrip() , but it's possible that you might get two arrays of integers where there is no match between a and b, and the function does not account for this condition. If this edge case happens, we'd find ourselves in an infinite loop, because the function would leave the for statement and never return. Because the return value is an int, you want to include a return statement outside of the for loop, before the final closing bracket },to avoid an infinite loop.
You start learning more about C++ and its basic syntax through online courses like https://www.learncpp.com/,, https://www.sololearn.com/Course/CPlusPlus/, and https://www.codecademy.com/learn/learn-c-plus-plus. I believe the first two are free, and the third lets you start with a free trial. Happy learning!
You should use compareTrip(a,b); instead of compareTrip(a[], b[]); as you are passing argument.
Your main function should return some value as it is interger type int main().
hope it helps.
Related
Problem Statement
|-O-| |-O-| |-O-| |-O-| |-O-|
Above you see a tiny fraction of our glorious imperial fleet, the Twin Ion Engine starfighters (known as "TIE fighters" for short).
Each TIE fighter consists of two solar array wings ('|'), two wing braces ('-') and one central cabin ('O').
Even though our TIE fighters are clearly superior to the X-wing fighters flown by those pesky rebels, occasionally some TIE fighter will receive some accidental combat damage. Whenever a damaged TIE fighter is recovered, we disasemble it and salvage the parts that are still in working condition. It is now up to you to use these salvaged parts to assemble as many complete TIE fighters as possible.
You are given the string salvagedParts. Each character of salvagedParts represents one piece of machinery that has been salvaged. As mentioned above, the characters '|', '-', and 'O' (uppercase oh) represent the parts you need to build a TIE fighter.
There may also be other characters in salvagedParts. These represent machinery that isn't used in a TIE fighter.
Class: TIEFighterAssembly
Method: assemble
Parameters: string
Returns: int
Method signature: int assemble(string salvagedParts)
(be sure your method is public)
Constraints:
salvagedParts will contain between 1 and 50 characters, inclusive.
Each character of salvagedParts will be one of "|-O=+()*" (quotes for clarity).
Examples
"|-O-|"
Returns: 1
One fully functional TIE fighter.
My code to this is
class TIEFighterAssembly
{
public:
int assemble(string salvagedParts)
{
int a=0,b=0,c=0;
for(int i=0;i<salvagedParts.length();i++)
{
if(salvagedParts[i]=='-')a++;
else if(salvagedParts[i]=='|')b++;
else if(salvagedParts[i]=='O')c++;
}
int sum=0;
while(c)
{
if(c>0&&a>=2&&b>=2)
{
sum++;
c--;
a-=2;
b-=2;
}
else c=0;
}
return sum;
}
};
I got this error
Your code did not compile:
errors compiling:
In file included from top level:3:0:
TIEFighterAssembly.cc:4:15: error: 'string' has not been declared
int assemble(string salvagedParts)
^
TIEFighterAssembly.cc: In member function 'int TIEFighterAssembly::assemble(int)':
TIEFighterAssembly.cc:7:31: error: request for member 'length' in 'salvagedParts', which is of non-class type 'int'
for(int i=0;i<salvagedParts.length();i++)
^
TIEFighterAssembly.cc:9:22: error: invalid types 'int[int]' for array subscript
if(salvagedParts[i]=='-')a++;
^
TIEFighterAssembly.cc:10:27: error: invalid types 'int[int]' for array subscript
else if(salvagedParts[i]=='|')b++;
^
TIEFighterAssembly.cc:11:27: error: invalid types 'int[int]' for array subscript
else if(salvagedParts[i]=='O')c++;
^
In file included from top level:10:0:
Your class or method was improperly declared: In function 'int _wrapper::thunk(std::string)':
Your class or method was improperly declared:20034:3: error: no matching function for call to 'TIEFighterAssembly::assemble(std::string&)'
Your class or method was improperly declared:20034:3: note: candidate is:
In file included from top level:3:0:
TIEFighterAssembly.cc:4:6: note: int TIEFighterAssembly::assemble(int)
int assemble(string salvagedParts)
^
TIEFighterAssembly.cc:4:6: note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'int'
In file included from top level:10:0:
Your class or method was improperly declared:20037:1: warning: control reaches end of non-void function [-Wreturn-type]
Can anyone help me how to submit a problem in TopCoder
Your method is correct, your code however can be modified to work without errors:
#include <string>
class TIEFighterAssembly
{
public:
static int assemble(std::string salvagedParts)
{
int a=0,b=0,c=0;
for(size_t i=0;i<salvagedParts.length();i++)
{
char character = salvagedParts[i];
if(character=='-')a++;
else if(character=='|')b++;
else if(character=='O')c++;
}
int sum=0;
while(c)
{
if(a>=2&&b>=2)
{
sum++;
c--;
a-=2;
b-=2;
}
else c=0;
}
return sum;
}
};
As others mentioned you used string instead of std::string. Also your code will have less overhead if you store the character first and then compare it.
Your method also can become simpler if you simply return the lesser variable from a,b and c:
#include <string>
class TIEFighterAssembly
{
public:
static int assemble(std::string salvagedParts)
{
int a=0,b=0,c=0;
for(size_t i=0;i<salvagedParts.length();i++)
{
char character = salvagedParts[i];
if(character=='-')a++;
else if(character=='|')b++;
else if(character=='O')c++;
}
b/=2;
a/=2;
return(a < b && a < c ? a : (b < c ? b : c));
}
};
When I run the following code, I meet some errors from leetcode.
class Solution {
public:
int findDuplicate(vector<int>& nums) {
int n = nums.size();
vector<int> dp(n, 0);
for(auto num:nums)
if(dp[num]==1)
return num;
else
dp[num]++;
}
};
solution.cpp: In member function findDuplicate
Line 5: Char 28: error: control reaches end of non-void function [-Werror=return-type]
vector<int> dp(n, 0);
^
cc1plus: some warnings being treated as errors
I have met the same error in some other questions before, thanks for your help.
You've declared findDuplicate to return an int.
If dp[num]==1 is never true, then your function doesn't return an int. In that case the behaviour of your program is undefined.
int findpow(int n1,int k, int count){ //while calling, k=1, count=0
if(k<n1)
return findpow(n1,k*2,count+1);
if(k==n1)
return count;
if(k>n1)
return --count;
}
This is a function that returns the largest power of two less than n. When I run it in my ubuntu terminal (g++ 4.8.4), it works fine. But when I am running it on www.hackerrank.com, it gives an error(control reaches end of non void function). The problem is, I participate in many contests on this website and I have come across this problem multiple times.
Please tell if you know how I can fix it.
You can use else if statement like this:
int findpow(int n1,int k, int count){ //while calling, k=1, count=0
if(k<n1)
return findpow(n1,k*2,count+1);
else if(k==n1)
return count;
else // Eliminate compiler errors (warnings)
return --count;
}
or as said #juanchopanza:
int findpow(int n1,int k, int count){ //while calling, k=1, count=0
if(k<n1)
return findpow(n1,k*2,count+1);
if(k==n1)
return count;
// Eliminate compiler errors (warnings)
return --count;
}
It will do the same thing as your code, but will not give a doubt to compiler that can be no return points from function.
'control reaches end of non void function' is a warning not an error, it's safe to ignore in this case but if you want to suppress the warning there are multiple ways:
put a return after the last condition
as Mykola suggested restructure the conditions to be explicit
set the -Wno-return-type flag
I have created a struct variable of type airplanes but so i have been trying to figure out why i get an error on the if states ""Airplanes" does not provide a subscript operator" also I am getting another error at part where i call the functions "No matching functions functions 'highest'" Can someone help me
count=highest(plane);
count2=lowest(plane);
}
int highest(airplanes plane){
int high=0;
int count=0;
for(int a =0;a<12;a++){
if(plane[a].averageDeparted>high){
count=a;
high=plane.averageDeparted;
}
}
return count;
}
int lowest(airplanes plane){
int low=100000;
int count2=0;
for(int a =0;a<20;a++){
if (plane[a].averageDeparted< low){
count2=a;
low=plane[a].averageDeparted;
}
}
return count2;
Based on your very belated comment, your parameter declarations are incorrect in both methods. They should be (airplanes planes[]) in both cases.
The type name is also incorrect, as each instance is clearly only one airplane.
Following code for max-heap implementation
#include<iostream>
#include<math.h>
using namespace std;
#define maxn 1000
int x[maxn];
int parent(int i){
return int(i/2);
}
int left(int i){
return 2*i;
}
int right(int i){
return 2*i+1;
}
void max_heap(int x[],int i,int size){
int largest;
int l=left(i);
int r=right(i);
if (l<=size && x[l]>x[i]){
largest=l;
}
else
{
largest=i;
}
if (r<=size && x[r]>x[largest]){
largest=r;
}
if (largest!=i) { int s=x[i];x[i]=x[largest];x[largest]=s;}
max_heap(x,largest,size);
}
int main(){
x[1]=16;
x[2]=4;
x[3]=10;
x[4]=14;
x[5]=7;
x[6]=9;
x[7]=3;
x[8]=2;
x[9]=8;
x[10]=1;
int size=10;
max_heap(x,2,size);
for (int i=1;i<=10;i++)
cout<<x[i]<<" ";
return 0;
}
When I run it, it writes such kind of warning:
1>c:\users\datuashvili\documents\visual studio 2010\projects\heap_property\heap_property\heap_property.cpp(36): warning C4717: 'max_heap' : recursive on all control paths, function will cause runtime stack overflow
Please tell me what is wrong?
The message tells you exactly what's wrong. You haven't implemented any checks to stop the recursion. One smart compiler.
max_heap function doesn't have base case, i.e., a return statement. You are just recursively calling the function but never saying when to break another successive call to the max_heap.
Also, in your example you are just calling the function with out satisfying any condition. Usually recursion is done or not done when a case is satisfied.
please tell me what is wrong?
Another problem that I see is that the size of your array x is 10. But the indices that you are using to set values are 1-10.
Put
max_heap(x,largest,size);
inside last check, like this:
if (largest!=i)
{
int s=x[i];
x[i]=x[largest];
x[largest]=s;
max_heap(x,largest,size);
}
and you're done!
There are many other problems with your code, but to answer your specific question, above change would do!