Page 1 of 6 123 ... LastLast
Results 1 to 25 of 142
Like Tree22Likes

Thread: The Code Marathon.

  1. #1
    Loquacious One Drsoccerball's Avatar
    Join Date
    May 2014
    HSC
    2015
    Gender
    Undisclosed
    Posts
    3,664
    Rep Power
    6

    The Code Marathon.

    Hey guys... I've noticed there hasn't really been any code type threads. So I've decided to make a code thread to post and solve problems.

    RULES:

    1. Try to solve all remaining problems before posting a new one.
    2. Try sticking with common languages such as c, java etc... ( So everyone can participate).
    3. Make your questions clear an concise.
    4. Attach images if needed.
    5. DO NOT post assignments looking for solutions.
    6. Try always leaving an active question after you solve someone elses.
    7. Label questions in numerical order.

    With all that down let's start with something relatively easy:

    Question 1 : Write a C program (or whatever language you use) that takes a single integer as argument and prints out a letter F of size n using * characters in EXACTLY the format shown below. You should also print error messages if there is invalid input.

    Example:

    ./letterF 4
    * * * *
    *
    *
    * * * *
    *
    *
    ./letterF 5
    * * * * *
    *
    *
    * * * * *
    *
    *
    *
    ./letterF 6
    * * * * * *
    *
    *
    *
    * * * * * *
    *
    *
    *
    Let the games begin.
    Last edited by Drsoccerball; 15 Jun 2016 at 4:43 PM.
    UNSWStudent likes this.

  2. #2
    New Member fffllappy's Avatar
    Join Date
    Oct 2015
    HSC
    N/A
    Gender
    Undisclosed
    Posts
    13
    Rep Power
    3

    Re: The Code Marathon.

    (assuming that printed lines will display on the line below)

    Pseudocode (not in the exact form as the one described, nor does it display error messages)

    BEGIN
    ___Input N
    ___Let mid = N/2 _______________(assuming that remainder is ignored)
    ___Create variable String = null
    ___FOR i = 1 TO N STEP 1
    ______String = String + "*"
    ___NEXT i
    ___Print String
    ___String = null
    ___FOR i = 2 TO N STEP 1
    ______IF i = mid
    _________FOR j = 2 TO N STEP 1
    ____________String = String + "*"
    _________NEXT j
    _________Print String
    ______ELSE
    _________print "*"
    ______ENDIF
    ___NEXT i
    END

  3. #3
    Supreme Member Flop21's Avatar
    Join Date
    May 2013
    HSC
    2015
    Gender
    Female
    Posts
    2,850
    Rep Power
    5

    Re: The Code Marathon.

    Wow fun. Will participate after my last exam.


    Also state what language you're writing in.
    turntaker likes this.
    2015 HSC: English Adv, Mathematics, Business Studies, Biology, Multimedia.

    HSC Biology Flashcards

  4. #4
    The optimistic pessimist. FlyingKanga's Avatar
    Join Date
    Dec 2012
    HSC
    2015
    Gender
    Male
    Posts
    412
    Rep Power
    5

    Re: The Code Marathon.

    I shall also join in on this after my exams
    turntaker likes this.

  5. #5
    Junior Member parad0xica's Avatar
    Join Date
    Mar 2016
    HSC
    N/A
    Gender
    Male
    Posts
    204
    Rep Power
    2

    Re: The Code Marathon.

    C code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]) {
      int i, j = 0, n = atoi(argv[1]);  
      while (j < 2) {
        // printing the "arm"
        for (i = 0; i < n; i++) {
          printf("*");
        }
        printf("\n");
        // printing the "spine"
        for (i = 0; i < n/2; i++) {
          printf("*\n");
        }
        j++;
      }
      if (n % 2 == 1) {
        printf("*\n");
      }
      return 0;
    }
    ___________________

    Question 2: write a program that finds the median of an unsorted array of 10 integer elements. You can generate the array however you wish, e.g. via command-line arguments, STDIN, looping.
    Last edited by parad0xica; 15 Jun 2016 at 8:29 PM.
    Clue 1: 121 112
    Who am I?

    ( ͡° ͜ʖ ͡°) ( ͡° ͜ʖ ͡°)Clue 2

    Clue 0: Clockwise

    Clue 3: Are you watching closely? ∃ a clue every now and then
    Clue 4: 99 115
    Clue 5: |℘(S)| = 15^2 + 31

  6. #6
    Loquacious One turntaker's Avatar
    Join Date
    May 2013
    HSC
    2015
    Gender
    Undisclosed
    Posts
    3,956
    Rep Power
    6

    Re: The Code Marathon.

    This is a good idea. Thanks.
    "I have crippling depression" -Mahatma Gandhi
    Quote Originally Posted by Katsumi View Post
    lol

  7. #7
    Loquacious One Drsoccerball's Avatar
    Join Date
    May 2014
    HSC
    2015
    Gender
    Undisclosed
    Posts
    3,664
    Rep Power
    6

    Re: The Code Marathon.

    Quote Originally Posted by parad0xica View Post
    Question 2: write a program that finds the median of an unsorted array of 10 integer elements. You can generate the array however you wish, e.g. via command-line arguments, STDIN, looping.
    Median of an even number refers to the average of the two middle numbers right ?

  8. #8
    Loquacious One turntaker's Avatar
    Join Date
    May 2013
    HSC
    2015
    Gender
    Undisclosed
    Posts
    3,956
    Rep Power
    6

    Re: The Code Marathon.

    Here is a Java solution for Question 1. (Uses Scanner for input)

    PHP Code:
    import java.util.*;

    public class 
    App {

        public static 
    void main(String[] args) {
            
    Scanner x = new Scanner(System.in);           //creating a new scanner object
            
    int length x.nextInt();                              //sets variable int to user input using the scanner object    
            
            
    for(int j 02j++){                                 //repeats loops below twice
                    
                
    for(int i 0length 1i++){            //printing horizontally        
                    
    System.out.print("*");
                }
                
                for(
    int i 0< (length 2) + 1;  i++){    //printing vertically
                    
    System.out.println("*");
                }
            
            }

        }

    edit: disregard the "PHP Code" title, just used it for syntax highlighting
    Last edited by turntaker; 15 Jun 2016 at 11:29 PM.
    "I have crippling depression" -Mahatma Gandhi
    Quote Originally Posted by Katsumi View Post
    lol

  9. #9
    Rambling Spirit
    Join Date
    Dec 2010
    HSC
    N/A
    Gender
    Female
    Posts
    6,965
    Rep Power
    10

    Re: The Code Marathon.

    1. Given a linked list, find the k-th element from the end, using only ONE single pass.

    For example, 1->2->3->4->5->X, the 0th element from the end is 5, 1st element from the end is 4 etc.

  10. #10
    Loquacious One turntaker's Avatar
    Join Date
    May 2013
    HSC
    2015
    Gender
    Undisclosed
    Posts
    3,956
    Rep Power
    6

    Re: The Code Marathon.

    wait once a problem has been solved, am I allowed to post a solution still, or do I move onto the next problem
    "I have crippling depression" -Mahatma Gandhi
    Quote Originally Posted by Katsumi View Post
    lol

  11. #11
    Loquacious One turntaker's Avatar
    Join Date
    May 2013
    HSC
    2015
    Gender
    Undisclosed
    Posts
    3,956
    Rep Power
    6

    Re: The Code Marathon.

    Quote Originally Posted by SpiralFlex View Post
    1. Given a linked list, find the k-th element from the end, using only ONE single pass.

    For example, 1->2->3->4->5->X, the 0th element from the end is 5, 1st element from the end is 4 etc.
    solution to Question 2 hasn't been posted yet
    "I have crippling depression" -Mahatma Gandhi
    Quote Originally Posted by Katsumi View Post
    lol

  12. #12
    Loquacious One Drsoccerball's Avatar
    Join Date
    May 2014
    HSC
    2015
    Gender
    Undisclosed
    Posts
    3,664
    Rep Power
    6

    Re: The Code Marathon.

    Quote Originally Posted by turntaker View Post
    wait once a problem has been solved, am I allowed to post a solution still, or do I move onto the next problem
    Well since it's in a different language I suppose you're allowed.
    turntaker likes this.

  13. #13
    Loquacious One turntaker's Avatar
    Join Date
    May 2013
    HSC
    2015
    Gender
    Undisclosed
    Posts
    3,956
    Rep Power
    6

    Re: The Code Marathon.

    Java is life,
    so much easier to read than C. (even though Java is C based)

    edit: actually nvm they are about the same in terms of reading difficulty lol
    "I have crippling depression" -Mahatma Gandhi
    Quote Originally Posted by Katsumi View Post
    lol

  14. #14
    Rambling Spirit
    Join Date
    Dec 2010
    HSC
    N/A
    Gender
    Female
    Posts
    6,965
    Rep Power
    10

    Re: The Code Marathon.

    Quote Originally Posted by turntaker View Post
    solution to Question 2 hasn't been posted yet
    The optimal solution to Q2 requires knowledge of data structures and algos, usually a semester 2 class. But most of you guys are in 1st year 1st sem.

  15. #15
    Ancient Orator leehuan's Avatar
    Join Date
    May 2014
    HSC
    2015
    Gender
    Male
    Posts
    5,811
    Rep Power
    7

    Re: The Code Marathon.

    Quote Originally Posted by SpiralFlex View Post
    The optimal solution to Q2 requires knowledge of data structures and algos, usually a semester 2 class. But most of you guys are in 1st year 1st sem.
    turntaker goes to UoW.
    turntaker likes this.

  16. #16
    Loquacious One turntaker's Avatar
    Join Date
    May 2013
    HSC
    2015
    Gender
    Undisclosed
    Posts
    3,956
    Rep Power
    6

    Re: The Code Marathon.

    Solution to Question 2. (if you meant the average of the two middle integers).

    PHP Code:

    import java
    .util.*;

    public class 
    App {

        public static 
    void main(String[] args) {
                
    ArrayList<IntegerarrayNums = new ArrayList<>();  //creating ArrayList 
                
    Random randomInt = new Random();    //creating Random object which generates a random  number
                
                
    for(int i 010i++ ){
                    
    int randomNum randomInt.nextInt(100);  //generating a Random int between 0 - 99
                    
                    
    arrayNums.add(randomNum);             //adding the numbers in the ArrayList
            
                
    }
                 
                for(
    int xarrayNums){                    //printing the whole array
                    
    System.out.print(" " x);
                    
                }
                
                
                
    double mean = (double)(arrayNums.get(4) + arrayNums.get(5)) / 2//getting the median of the ArrayList
                
    System.out.println();
                
    System.out.println(mean);            //printing the mean
                
        
    }

    "I have crippling depression" -Mahatma Gandhi
    Quote Originally Posted by Katsumi View Post
    lol

  17. #17
    Rambling Spirit
    Join Date
    Dec 2010
    HSC
    N/A
    Gender
    Female
    Posts
    6,965
    Rep Power
    10

    Re: The Code Marathon.

    Quote Originally Posted by Drsoccerball View Post
    Median of an even number refers to the average of the two middle numbers right ?

    Quote Originally Posted by turntaker View Post
    Solution to Question 2. (if you meant the average of the two middle integers).

    PHP Code:

    import java
    .util.*;

    public class 
    App {

        public static 
    void main(String[] args) {
                
    ArrayList<IntegerarrayNums = new ArrayList<>();  //creating ArrayList 
                
    Random randomInt = new Random();    //creating Random object which generates a random  number
                
                
    for(int i 010i++ ){
                    
    int randomNum randomInt.nextInt(100);  //generating a Random int between 0 - 99
                    
                    
    arrayNums.add(randomNum);             //adding the numbers in the ArrayList
            
                
    }
                 
                for(
    int xarrayNums){                    //printing the whole array
                    
    System.out.print(" " x);
                    
                }
                
                
                
    double mean = (double)(arrayNums.get(4) + arrayNums.get(5)) / 2//getting the median of the ArrayList
                
    System.out.println();
                
    System.out.println(mean);            //printing the mean
                
        
    }

    The array is unsorted, the median is not simply the average of two middle numbers.

  18. #18
    Loquacious One turntaker's Avatar
    Join Date
    May 2013
    HSC
    2015
    Gender
    Undisclosed
    Posts
    3,956
    Rep Power
    6

    Re: The Code Marathon.

    Quote Originally Posted by SpiralFlex View Post
    The array is unsorted, the median is not simply the average of two middle numbers.
    what does unsorted mean

    also I don't understand your given question
    "I have crippling depression" -Mahatma Gandhi
    Quote Originally Posted by Katsumi View Post
    lol

  19. #19
    Rambling Spirit
    Join Date
    Dec 2014
    HSC
    N/A
    Gender
    Male
    Posts
    6,048
    Rep Power
    7

    Re: The Code Marathon.

    It's not necessarily ordered from lowest to highest.
    Flop21 likes this.

  20. #20
    Loquacious One turntaker's Avatar
    Join Date
    May 2013
    HSC
    2015
    Gender
    Undisclosed
    Posts
    3,956
    Rep Power
    6

    Re: The Code Marathon.

    Quote Originally Posted by InteGrand View Post
    It's not necessarily ordered from lowest to highest.
    my array is not sorted
    "I have crippling depression" -Mahatma Gandhi
    Quote Originally Posted by Katsumi View Post
    lol

  21. #21
    Rambling Spirit
    Join Date
    Dec 2010
    HSC
    N/A
    Gender
    Female
    Posts
    6,965
    Rep Power
    10

    Re: The Code Marathon.

    Quote Originally Posted by turntaker View Post
    my array is not sorted
    You're solution assumes the middle elements (averaged) is the median, this is not the median.

  22. #22
    Loquacious One turntaker's Avatar
    Join Date
    May 2013
    HSC
    2015
    Gender
    Undisclosed
    Posts
    3,956
    Rep Power
    6

    Re: The Code Marathon.

    Quote Originally Posted by SpiralFlex View Post
    You're solution assumes the middle elements (averaged) is the median, this is not the median.
    What is the median
    "I have crippling depression" -Mahatma Gandhi
    Quote Originally Posted by Katsumi View Post
    lol

  23. #23
    Rambling Spirit
    Join Date
    Dec 2014
    HSC
    N/A
    Gender
    Male
    Posts
    6,048
    Rep Power
    7

    Re: The Code Marathon.

    Median means middle when the array is sorted so the numbers are in ascending order (taking average of two middle I case of even no. of elements).

    E.g. What is the median of 2,6,2,8,7? When sorted, the numbers are 2,2,6,7,8, so the median is 6 (middle number).

    ( https://www.mathsisfun.com/median.html )
    Last edited by InteGrand; 16 Jun 2016 at 12:03 AM.
    Nailgun likes this.

  24. #24
    Supreme Member Flop21's Avatar
    Join Date
    May 2013
    HSC
    2015
    Gender
    Female
    Posts
    2,850
    Rep Power
    5

    Re: The Code Marathon.

    Quote Originally Posted by turntaker View Post
    what does unsorted mean

    also I don't understand your given question
    You are confused by 'linked lists'? Or something else? Because it's understandable if you're confused with linked lists if you haven't been taught that concept yet.

    It's a way of storing data, you point to a 'node' which stores the value and the pointer to the next node... so on until it reaches the end (points to nothing/NULL).
    2015 HSC: English Adv, Mathematics, Business Studies, Biology, Multimedia.

    HSC Biology Flashcards

  25. #25
    Loquacious One turntaker's Avatar
    Join Date
    May 2013
    HSC
    2015
    Gender
    Undisclosed
    Posts
    3,956
    Rep Power
    6

    Re: The Code Marathon.

    Quote Originally Posted by InteGrand View Post
    Median means middle when the array is sorted so the numbers are in ascending order (taking average of two middle I case of even no. of elements).
    Ah ok, so i just need to sort my array and then find the average of the two middle numbers?
    "I have crippling depression" -Mahatma Gandhi
    Quote Originally Posted by Katsumi View Post
    lol

Page 1 of 6 123 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •