How to solve Command Line Arguments Coding Questions

TCS Command Line Programming Questions and Answers 2020-21

TCS has switched back to the command line programming for 2020 TCS NQT Exam. Last year this was excluded from the syllabus, but now they have included this topic again. You can expect one question from command line programming in the coding section

TCS NQT Command Line Programming 2020-21(1)

TCS Command Line Arguments Programs

This article is for TCS based command line Argument based Questions, you will get to know how to understand the the basic program and write it, Command Base Argument Coding for TCS, Command Line Arguments Questions for TCS.

TCS Coding RoundDetails
Number of questions2 questions
Time Alloted30 mins
Difficulty levelHigh

TCS CODING ROUND QUESTIONS IN TEST PATTERN AND SYLLABUS

  • Number of Questions – 2
  • Total time to Solve – 30 mins
  • Difficulty Level – High
  • Cut-off – Solve 1 question completely or partial output.

 

TCS Command Line Arguments Questions

Part 2

 

Command Line Arguments Questions for TCS Basics

  • Keywords like getc, scanf, getch, getchar etc can not be used.
  • Instead we use command line arguments to fetch the values.

Before reading further, I will suggest not to freak out if you don’t understand TCS Command Line Arguments Programs in first go. Read everything once and then when you read again things will start to make sense. It is the best resource on the internet to know about TCS command line Argument Type Questions in TCS C Compiler.

Command Line Arguments Questions for TCS

For TCS Command Line Arguments Programs let us consider this, if you wanted to write a basic C program then you would’ve written a main function that would’ve looked like – 

int main()
{
// some code in Command Line Arguments Questions for TCS C Compiler
}

However in command line arguments we write like this – 

int main(int argc, char *argv[]){

  1. argc – It is know as Argument Count and as clear from the name it stores the Count of number of Arguments.
  2. argv[] – Pointer, contains location of all the values(arguments).
  3. *argv[] – Array of values of all the arguments.
  4. They are parameters/arguments supplied to the program when it is invoked.

Thus, now we have two things for TCS C Compiler

  1. Total Count of number of Arguments.
  2. All the values/pointer location of arguments stored in an array.

Now, you will need one more thing i.e. atoi();

  • atoi(); – Converts string into int and atoi(argv[i]); will give the value of argument at ith location in int type format.

Now you can use an int val = atoi(argv[i]); to store the value and then print it with printf(); function.

To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments.

int main(int argc, char *argv[]) { /* ... */ }

or

int main(int argc, char **argv[]) { /* ... */ }

Quick Facts for TCS Command Line Arguments Programs

argv[0] contains the default value, not the input value so –

  • All for loops must start from i = 1.
  • You must use the following condition
if(argc == 1){
// do nothing since, there are no arguments, maybe ask for arguments?
}else{
// code to apply logic and print values.
}
  • provided+1 +1 for file.exe
  • argv[argc] is a NULL pointer.
  • argv[0] holds the name of the program.
  • argv[1] points to the first command line argument and argv[n] points last argument.

[table id=101 /]

Command Line Arguments Questions for TCS Rules for Coding Section Steps:
There is only one question for 20 minutes.

  1. It has 10 attempts(We can compile only 10 times).
  2. We must start our code from the scratch.
  3. The coding platform is divided into two, one for writing the code and other for output. We should write the whole program.
  4. We can’t use any input functions like scanf(), getch(), getchar().
  5. The input to be provided should be read as command line arguments.

[table id=102 /]

We must only print exact output in Command Line Arguments Questions for TCS Procedure –

  1. Output must not be re-framed by extra words.
  2. If there is any error, the error will be shown in the output dialog box.
  3. The errors are clearly mentioned.
  4. If there are no errors, a message like “compiled successfully” will be printed.
  5. Along with that they will mention four test cases are ‘passed’ or maybe ‘failed’.
  6. They are indicated like private and public test cases. They have not mentioned what is the test case, which is difficult to understand in TCS Command Line Arguments Programs.

Dont Compile again and again since compiler takes 25 seconds and each time you compile 25 seconds will become lesser in the time you have to code.

 

Most Asked Command Line Programs in TCS

You can scroll up to go to individual links of other asked programs but we first want you to learn command line programming above in the theory section and then come here to check most asked programs and then finally go to individual links to find other asked programs for command line.

Beware – Most websites are copying PrepInsta’s content/codes etc. Only trust us. If you see our copied content anywhere please contact us on Facebook and help us :).

Command Line Code for finding the Greatest Number

#include <stdio.h>
int main (int argc, char *argv[])
{

  int c[10];
  int i, temp, j, greatest;
  j = 0;
  for (i = 1; i < argc; i++)
   {
      temp = atoi (argv[i]);
      c[j] = temp;
      j++;
    }
  greatest = c[0];
  for (i = 0; i < 10; i++) { if (c[i] > greatest)
    	{
	         greatest = c[i];
    	}
    }
  printf ("Greatest of ten numbers is %d", greatest);
  return 0;
}

Command Line Code for Fibonacci Series

#include <stdio.h>
int main (int argc, char *argv[])
{
  int n, first = 0, second = 1, next, c;
  n = atol (argv[1]);
  printf ("First %d terms of Fibonacci series are :-\n", n);
  for (c = 0; c < n; c++)
    {
      if (c <= 1)
	    next = c;
      else
	{
	  next = first + second;
	  first = second;
	  second = next;
	}
      printf ("%d\n", next);
    }
  return 0;
}

Command Line Code for finding the Factorial of a number

#include<stdio.h>
int main(int argc, char *argv[])
{
 int n,i;
 unsigned long long factorial = 1;
 n = atol(argv[1]);
 for(i=1; i<=n; ++i)
  {
     factorial *= i;
  }
  printf("Factorial of %d = %llu", n, factorial);
}

Command Line Code for Reversal of a string

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char *argv[])
  {
      int k;
      char temp;
      int i,j=0;
      int strsize = 0;
      for (i=1; i<argc; i++) { strsize += strlen(argv[i]); if (argc > i+1)
          strsize++;
      }
      char *cmdstring;
      cmdstring = malloc(strsize);
      cmdstring[0] = '\0';
      for (k=1; k<argc; k++) { strcat(cmdstring, argv[k]); if (argc > k+1)
          strcat(cmdstring, " ");
      }
      i = 0;
      j = strlen(cmdstring) - 1;
      while (i < j)
      {
          temp = cmdstring[i];
          cmdstring[i] = cmdstring[j];
          cmdstring[j] = temp;
          i++;
          j--;
      }
      printf("\nReverse string is :%s", cmdstring);
      return(0);
  }

Command Line Code for Swapping of Numbers

#include<stdio.h>
int main(int argc, char *argv[])
  {
      double firstNumber, secondNumber, temporaryVariable;
      firstNumber = atol(argv[1]);
      secondNumber = atol(argv[2]);
      temporaryVariable = firstNumber;
      firstNumber = secondNumber;
      secondNumber = temporaryVariable;
      printf("\nAfter swapping, firstNumber = %.2lf\n", firstNumber);
      printf("After swapping, secondNumber = %.2lf", secondNumber);
      return 0;

}

3 comments on “How to solve Command Line Arguments Coding Questions”