Académique Documents
Professionnel Documents
Culture Documents
Contents
Background.......................................................................................................................................... 2 Pre-requisites before deep-diving into understanding input specifications ............................................ 2 Understanding input specifications ....................................................................................................... 3 Important Points ................................................................................................................................... 6
Background
The purpose of this document is to help CodeVita participants understand how to provide inputs to CodeVita Code Evaluation Platform. Far too many submissions from Practice round have ended in Compile Time errors and Run Time errors. In that sense, consider this document as a continuation to previous document titled Common Mistakes in using CodeVita Code Evaluation Platform. This earlier document is available for download from CodeVita portal (http://tcscodevita.com) as well as on Campus Commune at https://nextstep.tcs.com This document provides a sample problem text and provides its solution in C/C++/Java and C#. This will help participants to understand how to provide input to CodeVita.
Input 3 1 2 3
Output 6
3 10 10 10
30
(Note:-Please do not forget to end your program with a new line, else you may get a Compile Time Error from the Code Evaluation Engine)
The solutions to these problems which adhere to the input specification are written as shown below C
#include<stdio.h> #include<malloc.h> int main() { int *p,n,i,sum=0; scanf("%d",&n); p=(int*) malloc(n*sizeof(int)); for(i=1;i<=n;i++) { scanf("%d",&p[i]); sum=sum + p[i]; } free(p); printf("%d",sum); printf("\n"); return 0; }
C++
using namespace std; #include<iostream> int main() { int n,sum=0; cin>>n; int *p; p=new int[n]; for(int i=0;i<n;i++) { cin>>p[i]; sum+=p[i]; } cout<<sum<<endl; return 0; }
Java
import java.io.*; import java.util.Scanner; public class SumOfNumber { public static void main(String args[]) { int n,sum=0,arr[]; Scanner sc=new Scanner(System.in); n= sc.nextInt(); arr = new int[n]; for(int i=0; i<n; i++) { arr[i]=sc.nextInt(); } for(int i=0; i<n; i++) { sum+=arr[i]; } System.out.println(sum); } }
C#
using System; class Sum { static void { int n = for {
Main(string[] args) n,sum=0; Convert.ToInt32(Console.ReadLine()); (;n > 0;n--) int get = Convert.ToInt32(Console.ReadLine()); sum += get;
} Console.WriteLine(sum); } }
Note: - The above snippets are submitted by CodeVita participants who correctly understood and implemented the input specification. All these programs are Accepted in the system.
Important Points
1. Note how, C/C++ programs return 0 and end with a new line character. 2. Note how programs do not verbosely accept input i.e. there is no text like How many numbers you want to add? etc. The inputs specifications do not mention asking verbose questions. These programs do not ask those questions, but still read the input correctly, because they adhere to the specification.
3. Similarly, for programs which need absolute path of a file as inputs, do not print text like Enter absolute file path of the input file. 4. Note how, all the program start execution and then block on inputs using a. scanf() in C b. cin in C++ c. Scanner.nextXXX() in Java and, d. Console.ReadLine() in C# CodeVita platform expects the program to be running for it to provide the inputs. Your program has to be in a running state before CodeVita engine can give input(s) to it. If your program expects command-line arguments, then as soon as the program is launched, it will throw exceptions (because CodeVita does not provide inputs as command line arguments) and you will receive a Run-Time Error status message. 5. Once you read inputs from console, it is your responsibility to use appropriate functions from your language to convert it into data types needed to solve the problem. 6. Some of the CodeVita problems ask you to read the input file name from console. You should use techniques mentioned in points 4 & 5 above and then use appropriate file handling functions to read the files. You will never be asked to write to your output a file. Output always has to be printed to the console in specified format.