0% found this document useful (0 votes)
52 views5 pages

Homework Lecture 1 Programming Review: Import Class Public Static Void New Char Char New Char Int For Char

This document contains 5 programming tasks involving C++ code to: 1. Read a sentence from keyboard and print it in reverse order. 2. Read 4 points and determine the intersection between lines AB and CD, printing the intersection or "NO"/"MANY". 3. Read 2 numbers and find their greatest common divisor. 4. Read a list of numbers, sort them increasingly, and print the result. 5. Read 5 numbers, find the greatest and smallest, and print their sum.

Uploaded by

Le Thi Cam Nhung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views5 pages

Homework Lecture 1 Programming Review: Import Class Public Static Void New Char Char New Char Int For Char

This document contains 5 programming tasks involving C++ code to: 1. Read a sentence from keyboard and print it in reverse order. 2. Read 4 points and determine the intersection between lines AB and CD, printing the intersection or "NO"/"MANY". 3. Read 2 numbers and find their greatest common divisor. 4. Read a list of numbers, sort them increasingly, and print the result. 5. Read 5 numbers, find the greatest and smallest, and print their sum.

Uploaded by

Le Thi Cam Nhung
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Homework lecture 1

Programming review

*Note:
− Numbers in the same line must be separated by only one space.
− Float numbers must be rounded to 2 decimal places.

1. Your task is to write a C++ program to read a sentence from the


keyboard, and print to the screen the sentence in the reversed order.

Example

Keyboard Screen
hello olleh
how are you uoy era woh

import java.util.Scanner;
class MyClass {
    public static void main(String[ ] args) {
        Scanner input = new Scanner(System.in);
        String inputString = input.nextLine();
        char[] string = inputString.toCharArray();
        char[] res = new char [string.length];
        int index=string.length-1;
        for (char i:string)
        {
            res[index] = i;
            index--;
        }
        System.out.println(res);
    }
}
2. Your task is to write a C++ program including 2 classes Point and Line
to store four points (A, B, C, D) on the two-dimension plane. The
program determines the intersection between line AB and line CD.
Input: The first line contains two real numbers separated by a space
describing the point A. Similarly, the second, third, and fourth lines
contain data for points B, C, and D, respectively.
Output: Write to the screen the intersection point between AB and CD.
Write “NO” if there is no intersection, or “MANY” if AB and CD have
many intersection points.
Example
Keyboard Screen
00 0.50 0.50
11
01
10

import java.util.Scanner;
public class Point{
    public int x,y;
    Point(int x_ , int y_ ) {
        x = x_;
        y = y_;
    }
}
public class Line{
    public int a,b,c;
    Line(Point A, Point B){
        a = A.y - B.y;
        b = B.x - A.x;
        c = -(a*A.x+b*A.y);
    }
}
class MyClass {
    public static void main(String[ ] args) {
        Scanner in = new Scanner (System.in);
        int[][] arr = new int [4][2];
        for (int i=0; i< 4; i++){
            for (int j=0; j<2; j++){
                arr[i][j] = in.nextInt();}
        }
        Point A = new Point (arr[0][0],arr[0][1]);
        Point B = new Point (arr[1][0],arr[1][1]);
        Point C = new Point (arr[2][0],arr[2][1]);
        Point D = new Point (arr[3][0],arr[3][1]);

        Line AB = new Line(A,B);
        Line CD = new Line(C,D);

        double X, Y;
        if (AB.a*CD.b - CD.a*AB.b == 0 && AB.a*CD.c - CD.a*AB.c == 0){
            System.out.println("MANY");
        } else if (AB.a*CD.b - CD.a*AB.b == 0 && AB.a*CD.c - CD.a*AB.c 
!= 0){
            System.out.println("NO");
        } else {
            
            X = ((CD.c*AB.b - AB.c*CD.b)*1.00)/(AB.a*CD.b - CD.a*AB.b);
            Y = ((CD.a*AB.c - AB.a*CD.c)*1.00)/(AB.a*CD.b - CD.a*AB.b);
            System.out.println(X + " " + Y);
        }

    }
}

3. Your task is to write a C++ program to find the greatest common divisor
of two integer numbers.

Input: Read two positive integer numbers m and n from the keyboard
Output: Write to the screen the greatest common divisor of m and n.

Example
Keyboard Screen
96 3

import java.util.Scanner;
class MyClass {
    static int mygcd (int a, int b)
    {
        while (a*b != 0)
        {
            if (a>b) a=a%b;
            else b=b%a;
        }
        return a+b;
    }

    public static void main(String[ ] args) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        int res = mygcd(a,b);
        System.out.println(res);     
    }  
}
4. Your task is to write a C++ program to read a list of integer numbers
from the keyboard and write to the screen the list of numbers after being
increasingly sorted.

Input:
− The first line contains an integer number n that is the number of
numbers on the list.
− The second line contains n integer numbers separated by a space.

Output: Write to the screen n sorted numbers in one line.

Example
Keyboard Screen
5 23459
53429

import java.util.Scanner;
class MyClass {
    public static void main(String[ ] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[] arr = new int[n];
        for (int i=0; i<n; i++){
            arr[i] = input.nextInt();
        }
        // increasing sort
        for (int i=0; i<n-1; i++){
            for (int j=i+1; j<n; j++){
                if (arr[i] > arr[j])
                {
                    int temp = arr[i];
                    arr[i]=arr[j];
                    arr[j]=temp;
                }
            }
        }
        // print
        for (int i=0; i<n; i++){
            System.out.print(arr[i]+" ");
        }
    }
}
5. Given 5 different numbers, your task is to write a C++ program to
calculate the sum of the greatest number and the smallest number.
Input: One line contains 5 real numbers separated by a space.

Output: Write to the screen the sum of the greatest number and the
smallest number.

Example
Keyboard Screen
52429 11

import java.util.Scanner;
class MyClass {
    public static void main(String[ ] args) {
        Scanner input = new Scanner(System.in);
        int[] arr = new int [5];
        for (int i=0; i<5; i++){
            arr[i] = input.nextInt();
        }
        
        int max = arr[0], min = arr[0], res = 0;
        for (int i:arr)
        {
            if (i > max) max = i;
            if (i < min) min = i;
        }
        res = max + min;
        System.out.println(res);        
    }
}

You might also like