Fady Sadek
Fady Sadek

Reputation: 1140

codeforces runtime error on pset 1A

I am trying to solve problem 1A on codeforces

but i keep getting Test: #1, time: 0 ms., memory: 1828 KB, exit code: 1, checker exit code: 0, verdict: RUNTIME_ERROR you can check my entry here and find my code below , i tried to to run the program locally and it works fine and it passed the test case on the website

#include<stdio.h>
int calculateSquare(int n , int m , int a){
int length=0;
int width = 0;
if(n%a != 0){
    length = (n/a)+1 ;
}
else{
    length = n/a ;
}
 if(m%a != 0){
    width = (m/a)+1 ;
}
else{
    width = m/a ;
}

return length*width ;


 }
 void main(){
int n,m,a ;

scanf("%d %d %d",&n,&m,&a);
int output = calculateSquare(n,m,a);
printf("%d",output);
}

Upvotes: 0

Views: 3649

Answers (4)

Afjal Al Sayed
Afjal Al Sayed

Reputation: 1

This occurs when an uninitialized variable is passed as an Array or Vector size.

Code with error:

int m;
vector<int> arr(m);
//throws Exit code 2147483647
//default value of uninitialized int variable is 4201296 > 10^5
//so the vector<int> can't be created.

Correct answer:

int m;
cin>>m; //this works
vector<int> arr(m);

Upvotes: 0

user3629249
user3629249

Reputation: 16540

when trying to 'beat the clock', it is best to not use 'expensive' I.O functions.

Suggest the following two functions:

#include <stdio.h>

void fastRead( size_t *a );
void fastWrite( size_t a );

inline void fastRead(size_t *a)
{
    int c=0;
    // note: 32 is space character
    while (c<33) c=getchar_unlocked();

    // initialize result value
    *a=0;

    // punctuation parens, etc are show stoppers
    while (c>47 && c<58)
    {
        *a = (*a)*10 + (size_t)(c-48);
        c=getchar_unlocked();
    }
    //printf( "%s, value: %lu\n", __func__, *a );
} // end function: fastRead


inline void fastWrite(size_t a)
{
    char snum[20];
    //printf( "%s, %lu\n", __func__, a );

    int i=0;
    do
    {
        // 48 is numeric character 0
        snum[i++] = (char)((a%10)+(size_t)48);
        a=a/10;
    }while(a>0);

    i=i-1; // correction for overincrement from prior 'while' loop

    while(i>=0)
    {
        putchar_unlocked(snum[i--]);
    }
    putchar_unlocked('\n');
} // end function: fastWrite

Upvotes: 0

Jarvis
Jarvis

Reputation: 8564

I modified your code as given below which seems to be working fine :

#include <stdio.h>

long long int calculateSquare(long n , long m , long a){
  long length=0;
  long width = 0;
  if(n%a != 0){
    length = (n/a)+1 ;
  }
  else{
    length = n/a ;
  }
  if(m%a != 0){
    width = (m/a)+1 ;
  }
  else{
    width = m/a ;
  }
  long long store = length*widthl
  return store;
}

int main(){
  long int n,m,a ;
  scanf("%ld %ld %ld", &n, &m, &a);
  long int output = calculateSquare(n,m,a);
  printf("%ld\n", output);
  return 0;
}

Upvotes: 0

  • int calculateSquare(int n , int m , int a)

return type is int and return value is length*width

In the worst case a would be 1 and n, m would be 109 as stated in the problem

Input

The input contains three positive integer numbers in the first line: n,  m and a (1 ≤  n, m, a ≤ 109).

So the return type int cannot hold the returned value for such case.

Better using long long int if the compilation is conform with C99 standard.

Upvotes: 2

Related Questions