Thursday, July 15, 2021

You are given integers. Sort the integers and print the sorted order.

 You are given  integers.Sort the  integers and print the sorted order.

Store the  integers in a vector.Vectors are sequence containers representing arrays that can change in size.

  • Declaration:

    vector<int>v; (creates an empty vector of integers)
    
  • Size:

    int size=v.size();
    
  • Pushing an integer into a vector:

    v.push_back(x);(where x is an integer.The size increases by 1 after this.)
    
  • Popping the last element from the vector:

    v.pop_back(); (After this the size decreases by 1)
    
  • Sorting a vector:

    sort(v.begin(),v.end()); (Will sort all the elements in the vector)
    

To know more about vectors, Click Here

Input Format

The first line of the input contains  where  is the number of integers. The next line contains  integers.
Constraints

, where  is the  integer in the vector.

Output Format

Print the integers in the sorted order one by one in a single line followed by a space.

Sample Input

5
1 6 10 8 4

Sample Output

1 4 6 8 10
Answer:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    vector<int> v;
    int size,i;
    int number;
    cin>>size;
    for(i=0;i<size;i++)
       {
           cin>>number;
           v.push_back(number);
       }
       
    sort(v.begin(),v.end());
    
    int s = v.size();
    for(i=0;i<s;i++)
    {
        cout<<v[i]<<" ";
    }
    return 0;
}

 You have to create a class, named Student, representing the student's details, as mentioned above, and store the data of a student. Create setter and getter functions for each element; that is, the class should at least have following functions:

  • get_ageset_age
  • get_first_nameset_first_name
  • get_last_nameset_last_name
  • get_standardset_standard

Also, you have to create another method to_string() which returns the string consisting of the above elements, separated by a comma(,). You can refer to stringstream for this.

Input Format

Input will consist of four lines.
The first line will contain an integer, representing the age. The second line will contain a string, consisting of lower-case Latin characters ('a'-'z'), representing the first_name of a student.
The third line will contain another string, consisting of lower-case Latin characters ('a'-'z'), representing the last_name of a student.
The fourth line will contain an integer, representing the standard of student.

Note: The number of characters in first_name and last_name will not exceed 50.

Output Format

The code provided by HackerRank will use your class members to set and then get the elements of the Student class.

Sample Input

15
john
carmack
10

Sample Output

15
carmack, john
10

15,john,carmack,10
Answer:
#include <iostream>
#include <sstream>
using namespace std;

class Student{
    int age;
    string first_name;
    string last_name;
    int standard; 
    public
    void set_age(int age){
        this->age = age;
    }
    int get_age(){
        return age;
    }
    void set_first_name(string first_name)
    {
        this->first_name=first_name;
    }
    string get_first_name(){
        return first_name;
    }
    void set_last_name(string last_name){
        this->last_name=last_name;
    }
    string get_last_name(){
        return last_name;
    }
    void set_standard(int standard)
    {
        this->standard=standard;
    }
    int get_standard(){
        return standard;
    }
    string to_string()
    {
        stringstream s;
        char c = ',';
        s << age << c << first_name << c << last_name << c << standard;
        return s.str();
    }
};

int main() {
    int age, standard;
    string first_name, last_name;
    
    cin >> age >> first_name >> last_name >> standard;
    
    Student st;
    st.set_age(age);
    st.set_standard(standard);
    st.set_first_name(first_name);
    st.set_last_name(last_name);
    
    cout << st.get_age() << "\n";
    cout << st.get_last_name() << ", " << st.get_first_name() << "\n";
    cout << st.get_standard() << "\n";
    cout << "\n";
    cout << st.to_string();
    
    return 0;
}

Friday, July 9, 2021

 Structure in c

Input Format

Input will consist of four lines.
The first line will contain an integer, representing age.
The second line will contain a string, consisting of lower-case Latin characters ('a'-'z'), representing the first_name of a student.
The third line will contain another string, consisting of lower-case Latin characters ('a'-'z'), representing the last_name of a student.
The fourth line will contain an integer, representing the standard of student.

Note: The number of characters in first_name and last_name will not exceed 50.

Output Format

Output will be of a single line, consisting of agefirst_namelast_name and standard, each separated by one white space.

P.S.: I/O will be handled by HackerRank.

Sample Input

15
john
carmack
10

Sample Output

15 john carmack 10

Answer:

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

struct Student{
  int age;
  char first_name[51];
  char last_name[51]; 
  int standard;
};

int main() {
    Student st;
    
    cin >> st.age >> st.first_name >> st.last_name >> st.standard;
    
    cout << st.age << " " << st.first_name << " " << st.last_name << " " << st.standard;
    
    return 0;
}


Point to be noted:

When I made arrays for the first/last name of size 50, it failed on test case 3. When I made them 51, it passed. But the instructions specified it wouldn't exceed 50, so why do I need the extra 1 in my array?


Answer: It's because strings by default, have a null character '\0' at the end. So you have to reserve space for it too. So 50 + 1 = 51 characters.


Java Regex

You are updating the username policy on your company's internal networking platform. According to the policy, a username is considered v...