Arrays Introduction

Problem Statement

An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

Declaration:Movie Fifty Shades Darker (2017)

int arr[10]; //Declares an array named arr of size 10, i.e; you can store 10  integers.

Accessing elements of an array:

Indexing in arrays starts from 0.So the first element is stored at arr[0],the second element at arr[1]...arr[9]

You’ll be an given array of N integers and you have to print the integers in the reverse order.

Input Format

The first line of the input contains N,where N is the number of integers.The next line contains N integers separated by a space.

Constraints

1<=N<=1000

1<=Ai<=10000, where Ai is the ith integer in the array.

Output Format

Print the N integers of the array in the reverse order in a single line separated by a space.

Sample Input

4
1 4 3 2

Sample Output

2 3 4 1

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int n,i,a[max];
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
    for(i=n-1;i>=0;i--)
        cout<<a[i]<<" ";
    return 0;
}


Input and Output

Problem Statement

For any program that we write the basic things that we need to do is take the input and print the expected output.

In C++ you can take the input using cin and print the output using cout.Unlike C where you need the format specifier in printf and scanf, here you can use cin and cout.

Taking Input:

If you want to input a number: cin>>n ,  where n is the number.
If you want to input a number and a string: cin>>n>>s, where s is the string.

Printing output:

If you want to output a single number: cout<<n
If you want to output a number and a string separated by a new line: cout<<n<<endl<<s (where endl moves the pinter to the new line and then string gets printed.)

Take three numbers as inputs and print the sum of the three numbers.

Input Format

The first line of the input contains three integers A,B and C.

1A,B,C1000

Output Format

In a single line print the sum of the three numbers.

Sample Input

1 2 7

Sample Output

10

Explanation

Sum of the three numbers 1,2 and 7 is 10.

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int a,b,c;
    cin>>a>>b>>c;
    cout<<a+b+c;
    return 0;
}

For Loop

Problem Statement

A for loop is a programming language statement which allows code to be repeatedly executed.

The syntax for this is

for ( <expression_1> ; <expression_2> ; <expression_3> )
    <statement>
  • expression_1 is used for intializing variables which are generally used for controlling terminating flag for the loop.
  • expression_2 is used to check for the terminating condition. If this evaluates to false, then the loop is terminated.
  • expression_3 is generally used to update the flags/variables.

A sample loop will be

for(int i = 0; i < 10; i++) {
    ...
}

Input Format

You will be given two positive integers, a and b (ab), separated by a newline.

Output Format

For each integer n[a,b] (so all numbers in that range):

  • If 1n9, then print the English representation of it. That is “one” for 1, “two” for 2, and so on.
  • Else if n>9 and it is even, then print “even”.
  • Else if n>9 and it is odd, then print “odd”.

Note: [a,b] represents the interval, i.e., [a,b]={xZ| axb}={a, a+1,,b}

Sample Input

8
11

Sample Output

eight
nine
even
odd

Solution

  
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    // Complete the code.
    int a,b;
    cin>>a>>b;
    for(int n=a;n<=b;n++)
    {
     if(n>9)
       {
          if(n%2==0)
              cout<<"even"<<endl;
           else cout<<"odd"<<endl;
       }
    else if(n==9)
        cout<<"nine"<<endl;
    else if(n==1)
        cout<<"one"<<endl;
    else if(n==2)
        cout<<"two"<<endl;  
    else if(n==3)
        cout<<"three"<<endl;        
    else if(n==4)
        cout<<"four"<<endl; 
    else if(n==5)
        cout<<"five"<<endl;
    else if(n==6)
        cout<<"six"<<endl;
    else if(n==7)
        cout<<"seven"<<endl;      
    else
        cout<<"eight"<<endl; 
     
    }
    return 0;
}


Conditional Statements

Problem Statement

if and else are two of the most heavily used conditionals in C/C++. They are used to execute zero or one statement among many statements.

They are be used in the following three ways.

  1. if: It is used to execute a statement, given the condition is true.
    if(condition) {
        ...
    }
    
  2. if – else: It is used to execute exactly one of the two statements.
    if(first condition) {
        ...
    }
    else {
        ...
    }
    
  3. if – else if – else: It is used to execute one of the multiple statements.
    if(first condition) {
        ...
    }
    else if(second condition) {
        ...
    }
    .
    .
    .
    else if((n-1)'th condition) {
    
    }
    else {
        ...
    }
    

You are given a positive integer, n,:

  • If 1n9, then print the English representation of it. That is “one” for 1, “two” for 2, and so on.
  • Otherwise print “Greater than 9” (without quotes).

Input Format

Input will contain only one integer, n.

Output Format

Print the output as described above.

Sample Input

5

Sample Output

five

Sample Input #01

8

Sample Output #01

eight

Sample Input #02

44

Sample Output #02

Greater than 9

Solution


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int n;
cin>>n;
if(n>9)
cout<<"Greater than 9"<<endl;
else if(n==9)
cout<<"nine"<<endl;
else if(n==1)
cout<<"one"<<endl;
else if(n==2)
cout<<"two"<<endl;
else if(n==3)
cout<<"three"<<endl;
else if(n==4)
cout<<"four"<<endl;
else if(n==5)
cout<<"five"<<endl;
else if(n==6)
cout<<"six"<<endl;
else if(n==7)
cout<<"seven"<<endl;
else
cout<<"eight"<<endl;

return 0;
}

 

Pointer

Problem Statement

A pointer in C is a way to share a memory address among different contexts (primarily functions). They are primarily used whenever a function needs to modify the content of a variable, of which it doesn’t have ownership.

In order to access the memory address of a variable, val, we need to prepend it with & sign. E.g., &val returns the memory address of val.

This memory address is assigned to a pointer and can be shared among various functions. E.g. intp=&val will assign the memory address of val to pointer p. To access the content of the memory to which the pointer points, prepend it with a . For example, p will return the value reflected by val and any modification to it will be reflected at the source (val).

void increment(int *v) {
    (*v)++;
}

int main() {
    int a;
    scanf("%d", &a);
    increment(&a);
    printf("%d", a);
    return 0;
}  

You have to complete the function void update(int *a,int *b), which reads two integers as argument, and sets a with the sum of them, and b with the absolute difference of them.

  • a=a+b
  • b=|ab|

Input Format

Input will contain two integers, a and b, separated by a newline.

Output Format

You have to print the updated value of a and b, on two different lines.
P.S.: Input/ouput will be automatically handled. You only have to complete the void update(int *a,int *b) function.

Sample Input

4
5

Sample Output

9
1

Explanation

  • a=4+5=9
  • b=|45|=1

Solution

  
#include <stdio.h>
#include <stdlib.h>

void update(int *a,int *b) {
    // Complete this function 
    *a = *a+*b;
    *b = abs(*a-*b-*b);
}

int main() {
    int a, b;
    int *pa = &a, *pb = &b;
    
    scanf("%d %d", &a, &b);
    update(pa, pb);
    printf("%d\n%d", a, b);

    return 0;
}


Ruby Tutorial – Object Methods

Problem Statement

Each object in Ruby may have methods associated with it. To demonstrate this, we want you to print whether a number is even or odd. A number has an even? method associated with it, which returns true or false based on the parity of the number.

Assuming a variable number is already defined, check whether a given number is even or not.

Hint

Type in the code-editor

return number.even?

or

number.even?

Solution

  
# add your code here
return number.even?


Interchange two numbers

Problem Statement

You are given two integers. Store them into two variables and exchange them. Can it get any easier! Make sure you use tuple to do the task, rather than using any fancy logic. Print the two numbers.

Input Format

Two integers

Output Format

Two integers

Sample Input

2
1

Sample Output

1
2

Concept

A tuple is similar to a list. The difference is that a tuple is immutable i.e. it cannot be changed. Once you assign some value to a tuple, it cannot be changed. Also, no additional members can be added once a tuple is assigned.

Eg:
>> a = 1, # Define a tuple with one member
>> a = (2, 6)
>> a[1] = 1 # Can’t alter the elements in tuple
TypeError: ‘tuple’ object does not support item assignment

For traversing a tuple, the method is similar to that of a list. Tuples are most commonly used to assign more than one variables in one statement i.e. for simultaneous assignment.

Solution

  
# Enter your code here. Read input from STDIN. Print output to STDOUT
a=int(raw_input())
b=int(raw_input())
a,b=b,a
print a
print b

What’s Your Name?

Problem Statement

Let’s learn the basics of Python! You are given the first name and the last name of a person. Your task is to read them and print the following:

Hello firstname lastname! You just delved into python.

It’s that simple!

In Python you can read a line as a string using

s = raw_input()
#here s reads the whole line.  

Input Format The first line contains the first name, and the second line contains the last name.

Constraints
The length of the first and last name ≤ 10.

Output Format Print the output as mentioned above.

Sample Input

Guido
Rossum

Sample Output

Hello Guido Rossum! You just delved into python.

Concept The input read by the program is stored as a string data type. A string is a collection of characters.

Solution

  
# Enter your code here. Read input from STDIN. Print output to STDOUT
f = raw_input()
l = raw_input()
print "Hello" , f ,l + "! You just delved into python."