Search This Blog

Sample Programming Problems


C and C++ Programming Practice Problems

Many of these problems will also make for excellent C++ job interview preparation. Fill in the blank exercises are designed for true beginners, where a large portion of the code is already provided!

Fill in the blank exercises

Fill in the missing parts of the code to create a working program that will accomplish the stated task.
When completed, the following program should first fill in (by asking the user for input) and then list all the elements in an array:
#include <_____>
      
      using namespace std;
      ___ main()
{
  int array[8]___
  for(int x=_; x<_; ___)
  cin>>______;
  for(____; x<____; ___)
  cout<<_____;
  return __;

Guessing Game Solution
The following program will act as a guessing game in which the user has eight tries to guess a randomly generated number. The program will tell the user each time whether he guessed high or low:
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
int number=rand()___; 
int
guess=-1;
int trycount=0;
while(guess__number __ trycount<8)
{

cout__"Please enter a guess: ";
cin__guess;
__(guess_number)

cout<<"Too low"<<endl;
 __(guess_number)

cout<<"Too high"<<endl;
_______________;
_

if(guess==number)
cout<<"You guessed the number";
____

cout<<"Sorry, the number was: "<<_____;
______
0;



Calculator Challenge

The following program should function as a basic calculator; it should ask the user to input what type of arithmetic operation he would like, and then ask for the numbers on which the operation should be performed. The calculator should then give the output of the operation.
_______ <iostream>


___ multiply(int x, int y)
{
  ______ x_y;
}


____ divide(int x, int y)
{
  _____ x_y;
}

_____ add(int x, int y) 
{
  ______x_y;
}

______ subtract(int x, int y)
{
  _____x_y;
}


using namespace std;

___ _____()
{
  ____ op='c';
  ____ x, y;
  while(op!='e')
  {
  cout__"What operation would you like to perform: add(+), subtract(-), divide(/), 
  multiply(*), [e]xit?";
  cin__op;
  switch(op)
  {
    ____ '+':
    cin__x;
    cin__y;
    cout__x__"+"__y__"="__add(x, y)__endl_
    break;
    ____ '-'_
    cin__x;
    cin__y;
    cout__x__"-"__y__"="__subtract(x, y)__endl_
    break;
    ____ '/':
    cin__x;
    cin__y;
    cout__x__"/"__y__"="__divide(x, y)__endl_
    break;
    ____ '*'_
    cin__x;
    cin__y;
    cout__x__"*"__y__"="__multiply(x, y)__endl_
    break;
    _____ 'e':
    ______;
    ______:
    cout__"Sorry, try again"__endl;
    }
  }
  return _;

Basic Programming Challenges


Temperature Converter Challenge

Credit: Denis Meyer
In this challenge, write a program that takes in three arguments, a start temperature (in Celsius), an end temperature (in Celsius) and a step size. Print out a table that goes from the start temperature to the end temperature, in steps of the step size; you do not actually need to print the final end temperature if the step size does not exactly match. You should perform input validation: do not accept start temperatures less than a lower limit (which your code should specify as a constant) or higher than an upper limit (which your code should also specify). You should not allow a step size greater than the difference in temperatures. (This exercise was based on a problem from C Programming Language).


Sample run:
  Please give in a lower limit, limit >= 0: 10
  Please give in a higher limit, 10 > limit <= 50000: 20
  Please give in a step, 0 < step <= 10: 4

  Celsius         Fahrenheit
  -------         ----------
  10.000000       50.000000
  14.000000       57.200000
  18.000000       64.400000
  

Line Count Programming Challenge

Line Count Challenge
Here's a simple help free challenge to get you started: write a program that takes a file as an argument and counts the total number of lines. Lines are defined as ending with a newline character. Program usage should be
count filename.txt
and the output should be the line count.

File Size Challenge

Credit: Denis Meyer
In this challenge, given the name of a file, print out the size of the file, in bytes. If no file is given, provide a help string to the user that indicates how to use the program. You might need help with taking parameters via the command line or file I/O in C++ (if you want to solve this problem in C, you might be interested in this article on C file I/O). 

Challenges - Permutation Challenge

String Permutation Challenge
Here is another mathematical problem, where the trick is as much to discover the algorithm as it is to write the code: write a program to display all possible permutations of a given input string--if the string contains duplicate characters, you may have multiple repeated results. Input should be of the form
permute string
and output should be a word per line.
Here is a sample for the input cat
cat
cta
act
atc
tac
tca

Intermediate Programming Challenges


Integer to English number conversion

Write a program that takes an integer and displays the English name of that value.
You should support both positive and negative numbers, in the range supported by a 32-bit integer (approximately -2 billion to 2 billion).
Examples:
10 -> ten
121 -> one hundred twenty one
1032 -> one thousand thirty two
11043 -> eleven thousand forty three
1200000 -> one million two hundred thousand

Challenge - Factorial Challenge

Factorial Challenge
Here's a challenge that's a bit more mathematical in nature. Write a program that determines the number of trailing zeros at the end of X! (X factorial), where X is an arbitrary number. For instance, 5! is 120, so it has one trailing zero. (How can you handle extremely values, such as 100!?) The input format should be that the program asks the user to enter a number, minus the !. 

Challenges - String Search Challenge

String Searching Challenge
Write a program that takes two arguments at the command line, both strings. The program checks to see whether or not the second string is a substring of the first (without using the substr -- or any other library -- function). One caveat: any * in the second string can match zero or more characters in the first string, so if the input were abcd and the substring were a*c, then it would count as a substring. Also, include functionality to allow an asterisk to be taken literally if preceded by a \, and a \ is taken literally except when preceding an asterisk.


Programming Challenges - Number Base Conversion

String Searching Challenge
Write a program that accepts a base ten (non-fractional) number at the command line and outputs the binary representation of that number. Sample input is
dectobin 120

Challenges - Pascal's Triangle Challenge

Pascal's Triangle Challenge
Write a program to compute the value of a given position in Pascal's Triangle (See image).
The way to compute any given position's value is to add up the numbers to the position's right and left in the preceding row. For instance, to compute the middle number in the third row, you add 1 and 1; the sides of the triangle are always 1 because you only add the number to the upper left or the upper right (there being no second number on the other side).
The program should prompt the user to input a row and a position in the row. The program should ensure that the input is valid before computing a value for the position.

Linked List Reverse Printing Challenge

Using whatever programming techniques you know, write the cleanest possible function you can think of to print a singly linked list in reverse. The format for the node should be a struct containing an integer value, val, and a next pointer to the following node.

Linked List In-Place Reverse Challenge

This challenge, similar to the last linked list challenge, involves reversing a singly linked list--but this time, you must not out the list but actually reverse the entire list in place. By in-place, I mean that no memory can be allocated. The resulting code should be function that takes the head of a list and returns a the new head of the reversed list.

Advanced Programming Challenges

Array Sum Challenge

In this challenge, given an array of integers, the goal is to efficiently find the subarray that has the greatest value when all of its elements are summed together. Note that because some elements of the array may be negative, the problem is not solved by simply picking the start and end elements of the array to be the subarrray, and summing the entire array.

For example, given the array
  {1, 2, -5, 4, -3, 2}
  
The maximum sum of a subarray is 4. It is possible for the subarray to be zero elements in length (if every element of the array were negative). 

Before you write the code, take some time to think of the most efficient solution possible; it may surprise you. The major goal of this challenge is to test your algorithmic skills rather than merely your ability to write code quickly.




Self-Printing Program

Write a program that, when run, will print out its source code. This source code, in turn, should compile and print out itself. (Fun fact: a program that prints itself is called a quine.)

No comments:

Post a Comment