• Congratulations to the Class of 2024 on your results!
    Let us know how you went here
    Got a question about your uni preferences? Ask us here

The Code Marathon. (4 Viewers)

Drsoccerball

Well-Known Member
Joined
May 28, 2014
Messages
3,650
Gender
Undisclosed
HSC
2015
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:

fffllappy

New Member
Joined
Oct 6, 2015
Messages
13
Gender
Undisclosed
HSC
N/A
(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
 

Flop21

Well-Known Member
Joined
May 12, 2013
Messages
2,807
Gender
Female
HSC
2015
Wow fun. Will participate after my last exam.


Also state what language you're writing in.
 

parad0xica

Active Member
Joined
Mar 24, 2016
Messages
204
Gender
Male
HSC
N/A
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:

Drsoccerball

Well-Known Member
Joined
May 28, 2014
Messages
3,650
Gender
Undisclosed
HSC
2015
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 ?
 

turntaker

Well-Known Member
Joined
May 29, 2013
Messages
3,908
Gender
Undisclosed
HSC
2015
Here is a Java solution for Question 1. (Uses Scanner for input)

PHP:
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 = 0; j < 2; j++){                                 //repeats loops below twice
		        
			for(int i = 0; i < length - 1; i++){		    //printing horizontally		
				System.out.print("*");
			}
			
			for(int i = 0; i < (length / 2) + 1;  i++){    //printing vertically
				System.out.println("*");
			}
		
		}

	}
}
edit: disregard the "PHP Code" title, just used it for syntax highlighting
 
Last edited:

SpiralFlex

Well-Known Member
Joined
Dec 18, 2010
Messages
6,960
Gender
Female
HSC
N/A
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.
 

turntaker

Well-Known Member
Joined
May 29, 2013
Messages
3,908
Gender
Undisclosed
HSC
2015
wait once a problem has been solved, am I allowed to post a solution still, or do I move onto the next problem
 

turntaker

Well-Known Member
Joined
May 29, 2013
Messages
3,908
Gender
Undisclosed
HSC
2015
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
 

turntaker

Well-Known Member
Joined
May 29, 2013
Messages
3,908
Gender
Undisclosed
HSC
2015
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
 

turntaker

Well-Known Member
Joined
May 29, 2013
Messages
3,908
Gender
Undisclosed
HSC
2015
Solution to Question 2. (if you meant the average of the two middle integers).

PHP:
import java.util.*;

public class App {

	public static void main(String[] args) {
			ArrayList<Integer> arrayNums = new ArrayList<>();  //creating ArrayList 
			Random randomInt = new Random();    //creating Random object which generates a random  number
			
			for(int i = 0; i < 10; i++ ){
				int randomNum = randomInt.nextInt(100);  //generating a Random int between 0 - 99
				
				arrayNums.add(randomNum); 			//adding the numbers in the ArrayList
		
		    }
			 
			for(int x: arrayNums){					//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
			
	}
}
 

SpiralFlex

Well-Known Member
Joined
Dec 18, 2010
Messages
6,960
Gender
Female
HSC
N/A
Median of an even number refers to the average of the two middle numbers right ?

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

PHP:
import java.util.*;

public class App {

	public static void main(String[] args) {
			ArrayList<Integer> arrayNums = new ArrayList<>();  //creating ArrayList 
			Random randomInt = new Random();    //creating Random object which generates a random  number
			
			for(int i = 0; i < 10; i++ ){
				int randomNum = randomInt.nextInt(100);  //generating a Random int between 0 - 99
				
				arrayNums.add(randomNum); 			//adding the numbers in the ArrayList
		
		    }
			 
			for(int x: arrayNums){					//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.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 4)

Top