HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

Chef and Digits

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
anddigitschef

Problem

I am looking for a review on the following code which is for this question. I am looking for a general review of the code. I am specifically also looking for advice on how to make this code run more efficiently.

#include
#include
using namespace std;
long int b[100001]={0},x,n,m,i;
char num[100001];  
int main() 
{ 
        cin>>n>>m;
        for(i=1;i>num[i];
     while(m--)
    {
         b[100001]={0};
         cin>>x;
         for(i=1;i<x;i++)
            {
              b[i]=b[i-1]+abs(num[x]-num[i]);
            }
          cout<<b[x-1]<<"\n";
    }
   return 0;
}

Solution

-
Try not to get into the habit of using using namespace std. It may be okay for small programs, but it can cause problems for larder ones.

-
Use ` instead of ; the latter is a C library.

Moreover, you don't need this library at all;
is where std::abs() is defined.

-
Even in a small program, do not use global variables:

long int b[100001]={0},x,n,m,i;
char num[100001];


The problem with this is that these variables can be changed anywhere in the program. If you have a large program, this will be even worse as it won't be easy to determine where a certain variable is being modified. I'd strongly recommend that you don't let this become a habit.

If you're going to keep everything in
main() (could be permissible for this small program), then move those variables into the function. If you were to expand this to multiple functions, then pass those variables to the functions from main().

-
Avoid single-character variable names unless they're simple loop counters. This makes it harder for others to understand your code. It will also make maintenance a lot harder, especially if you decide to grow this program. Use more distinctive names that will also not require comments.

-
Use whitespace between operators for readability. There's no need to cram characters together if you're trying to have shorter lines. Just find ways to use less code on each line.

This, for instance:

b[i]=b[i-1]+abs(num[x]-num[i]);


will become

b[i] = b[i-1] + abs(num[x] - num[i]);


-
Declaring the loop counter outside the loop statement is from C, not C++. Declare it inside.

for (int i = 0; i < n; i++)


-
You don't need the explicit
return 0 at the end of main(). Reaching this point already implies successful termination, so the compiler will insert it for you.

-
Although it may not make a great difference here, I'd recommend considering
std::vector over C-style arrays in general. This is a common C++ storage container and is more usable than static arrays as it's a dynamic container.

Your C-style arrays:

long int b[100001]={0};


char num[100001];


would look like these respectively:

std::vector b(100001, 0);


std::vector num(100001);


This container overloads
operator[]`, so the syntax for accessing C-style array elements is the same. But you also get iterators, which directly point to the elements and will help prevent accessing the container out of bounds.

Code Snippets

long int b[100001]={0},x,n,m,i;
char num[100001];
b[i]=b[i-1]+abs(num[x]-num[i]);
b[i] = b[i-1] + abs(num[x] - num[i]);
for (int i = 0; i < n; i++)
long int b[100001]={0};

Context

StackExchange Code Review Q#46547, answer score: 12

Revisions (0)

No revisions yet.