Assignment 8b - Function

1. Write a C program to input a two-digit integer, swap its digits and then return the result to the sample output. The main body of your program is shown below.

int main(void)
{
        int x, y;

        read(x);
        swap(x);
        write(x);

        return 0;
}

	
   
2. Write a C program to input 2 integers, swap their values and then return the result to the sample output. The main body of your program is shown below.

int main(void)
{
        int x, y;

        read(x,y);
        swap(x,y);
        write(x,y);

        return 0;
}

    	
   
3. Write a C program to input 2 strings, swap their values and then return the result to the sample output. The main body of your program is shown below.

int main(void)
{
        string x, y;

        read(x,y);
        swap(x,y);
        write(x,y);

        return 0;
}

	
   
4. Write a C program to input a string, put the characters in reverse order and then return the result to the sample output. The main body of your program is shown below.

int main(void)
{
        string x;

        read(x);
        reverse(x);
        write(x);

        return 0;
}

	
   
5. Write a C program to find the position of a number in the array. The main body of your program is shown below.

#include 
using namespace std;

#define size 6

int main(void)
{
        int arr[6] = {17, 23, 89, 60, 45, 52};
        int k;

        while(true){
                read(k);
                search(arr,k);
        }
        return 0;
}

	

Sample Output

Enter the number = 80
Not found
Enter the number = 23
It is at position 1
:
:
   
6. (move to chapter 10) Write a program to input an array of integers, sort it in ascending order and then return the array to the sample output. The main body of your program is shown below.

int main(void)
{
        int arr[10];
        int k;

        while(true){
                read(arr);
                sort(arr);
		print(arr);
        }
        return 0;
}
	
   
7.

An encryption method consists of the following sequence of transformations:

Define the functions: ‘next’, ‘shift’ and ‘swap’ so that the following program can change the plain text "computer" to the cipher text "dsnpvqfu".


int main(void)
{
        string x;
        read(x);
        next(x);
        shift(x);
        swap(x);

        return 0;
}

   
8.

Write a C program to insert, delete and update numbers in the one-dimensional array as defined below. You have to use function in this program.

int number[100]={36, 81, 45, 17, 69, 3, 23, 92, 70, 54};
int count = 10;

Sample Output
Number sequence: 36, 81, 45, 17, 69, 3, 23, 92, 70, 54,

1. Insert number
2. Delete number
3. Update number
4. Exit

Option = 3

Array index = 2
Reset value = 123

Number sequence: 36, 81, 123, 17, 69, 3, 23, 92, 70, 54,

1. Insert number
2. Delete number
3. Update number
4. Exit

Option = 1

Array index = 6
New element = 800

Number sequence: 36, 81, 123, 17, 69, 3, 800, 23, 92, 70, 54,

1. Insert number
2. Delete number
3. Update number
4. Exit

Option = 2

Array index = 9

Number sequence: 36, 81, 123, 17, 69, 3, 800, 23, 92, 54,

:

  main function
 

int main(void)
{

        int number[100]={36, 81, 45, 17, 69, 3, 23, 92, 70, 54};
        int count = 10, opt;
        do{
                printseq(number, count);
                readopt(opt);
                if(opt==1)
                        insertnum(number, count);
                else if(opt==2)
                        deletenum(number,count);
                else if(opt==3)
                        updatenum(number,count);
                else
                        cout << "Bye" << endl;
        }while(opt!=4);
        return 0;
}

	
11.

< A simplified scrabble program >

Write a C program to put words on a 12 x 12 chess board. You have to use function in this program.

Sample Output

0
1
2
3
4
5
6
7
8
9
10
11
0
-
-
-
-
-
-
-
-
-
-
-
-
1
-
-
-
-
-
-
-
-
-
-
-
-
2
-
-
-
-
-
-
-
-
-
-
-
-
3
-
-
-
-
-
-
-
-
-
-
-
-
4
-
-
-
-
-
-
-
-
-
-
-
-
5
-
-
-
-
-
-
-
-
-
-
-
-
6
-
-
-
-
-
-
-
-
-
-
-
-
7
-
-
-
-
-
-
-
-
-
-
-
-
8
-
-
-
-
-
-
-
-
-
-
-
-
9
-
-
-
-
-
-
-
-
-
-
-
-
10
-
-
-
-
-
-
-
-
-
-
-
-
11
-
-
-
-
-
-
-
-
-
-
-
-

Enter a word: computer
Horizontal (1) or Vertical (2): 2
Row and Col number: 2 6

0
1
2
3
4
5
6
7
8
9
10
11
0
-
-
-
-
-
-
-
-
-
-
-
-
1
-
-
-
-
-
-
-
-
-
-
-
-
2
-
-
-
-
-
-
c
-
-
-
-
-
3
-
-
-
-
-
-
o
-
-
-
-
-
4
-
-
-
-
-
-
m
-
-
-
-
-
5
-
-
-
-
-
-
p
-
-
-
-
-
6
-
-
-
-
-
-
u
-
-
-
-
-
7
-
-
-
-
-
-
t
-
-
-
-
-
8
-
-
-
-
-
-
e
-
-
-
-
-
9
-
-
-
-
-
-
r
-
-
-
-
-
10
-
-
-
-
-
-
-
-
-
-
-
-
11
-
-
-
-
-
-
-
-
-
-
-
-

Enter a word: internet
Horizontal (1) or Vertical (2): 1
Row and Col number: 7 4

0
1
2
3
4
5
6
7
8
9
10
11
0
-
-
-
-
-
-
-
-
-
-
-
-
1
-
-
-
-
-
-
-
-
-
-
-
-
2
-
-
-
-
-
-
c
-
-
-
-
-
3
-
-
-
-
-
-
o
-
-
-
-
-
4
-
-
-
-
-
-
m
-
-
-
-
-
5
-
-
-
-
-
-
p
-
-
-
-
-
6
-
-
-
-
-
-
u
-
-
-
-
-
7
-
-
-
-
i
n
t
e
r
n
e
t
8
-
-
-
-
-
-
e
-
-
-
-
-
9
-
-
-
-
-
-
r
-
-
-
-
-
10
-
-
-
-
-
-
-
-
-
-
-
-
11
-
-
-
-
-
-
-
-
-
-
-
-

:

   
10.

< Tic-Tac-Toe >

Structure Diagram: link , Program Template: link,

Sample Output

0
1
2
3
4
5
6
7
8
9
10
11
0
-
-
-
-
-
-
-
-
-
-
-
-
1
-
-
-
-
-
-
-
-
-
-
-
-
2
-
-
-
-
-
-
-
-
-
-
-
-
3
-
-
-
-
-
-
-
-
-
-
-
-
4
-
-
-
-
-
-
-
-
-
-
-
-
5
-
-
-
-
-
-
-
-
-
-
-
-
6
-
-
-
-
-
-
-
-
-
-
-
-
7
-
-
-
-
O
-
-
-
-
-
-
-
8
-
-
-
-
O
-
-
-
-
-
-
-
9
-
-
-
X
X
O
-
-
-
-
-
-
10
-
-
-
O
X
X
O
-
-
-
-
-
11
-
-
-
O
X
X
O
X
-
-
-
-

 

   
11.

Rewrite the following C program by using function: Assignment 5 Question 12

   
12. Rewrite the following C program by using function: Assignment 7 Question 9