Sie sind auf Seite 1von 8

Note : In all the programs using functions, the functions should not print out any values except

for those
printf statements used for debugging purposes.

Do not use Global variables for any of the solutions.

Assume user will enter valid data for all requested inputs; no need to validate any input.

Q1
Write a program that will pass an one dimensional array of integers to a function that will reverse the
elements in that array. The main program should display the elements of the original array before the
reversal and after the reversal. The function should be able to handle any size array.

Assume the array will be initialized with some values before calling the function.

Example output

Scenario 1

Before change
1537

After change
7351

Secnario 2

Before change
15372

After change
27351

Q2
Write a C program that will pass a one dimensional array of characters to a function that will reverse
the elements in that array. The main program should display the elements of the original array before
the reversal and after the reversal. The function should be able to handle any size array.

Assume the array will be initialized with some values (no more than 100) before calling the function.

Example output

Scenario 1, if value of “pan” is used

Before change
pan
After Change
nap

Scenario 2, if value of “gold” is used

Before change
gold
After Change
dlog

Q3
Write a complete C program that will call a function that accepts three one dimensional arrays of
integers. The function should add the two arrays and place the result into the third array. The
function should be able to work with any array size.

The main progam should display the three arrays before the function call and after the function call to
verify the addition worked.

For programming simplicity, use values shown in the example below to the arrays. The ? denotes
unknown or any value. However, the main program and function should work with any array size and
initial values configurable in the program.

Expected output

Array 1 : 2 4 6 8
Array 2: 4 3 2 1
Result : ? ? ? ?

Result after the addition call

Array 1 : 2 4 6 8
Array 2: 4 3 2 1
Result : 6 7 8 9

Q4
Write a program that will call a function that accepts three one dimensional arrays of characters (you
decide on the size and the initial elements). The function should interleave the arrays and include an
“*” in the resulting output. The resulting output should be placed into the third array.

Write a program that has a main function and a utility function called merge. The main function
should prompt the user to enter two different strings ( no more than 50 character each). It should call
the merge function that will interleave the first string and the second string and include an “*” to
produce a third string. The merge stops with the shortest string. The main program should print out
all the three strings after the function call as shown below.
Scenario 1
Enter string 1 : ABC
Enter string 2: 12345

Result after merge call

String 1 : ABC
String 2 : 12345
Merged string : A1*B2*C3*

Scenario 2
Enter string 1 : ABCDE
Enter string 2: 1234

Result after merge call

String 1 : ABCDE
String 2 : 1234
Merged string : A1*B2*C3*D4*

Q5
Create a function that accepts a two dimensional array of integers and displays
a) Row by row ( fix row - iterate thru the columns)
b) b) column by column (fix column – iterate thru the rows)

Lets assume the elements of the array 3 X 4 is

11 12 13 14
21 22 23 24
31 32 33 34

Example output for a)


11 12 13 14 *(these number are from row 1)
21 22 23 24 *(these number are from row 2)
31 32 33 34

Example output for b)


11 21 31 *(these number are from column 1)
12 22 32 *(these number are from column 2)
13 23 33
14 24 34

*Note : This is information for you .. do not display this as part of your output
Q6
Write a C program that calls a function to add two matrices (matrix = two dimensional array).

You decide on the size of the matrix and the elements of the matrices.

The main program should create and populate the matrices with non-zero values. The function should
perform a matrix addition and assign the result to another array. The function should be flexible
enough to work with any array size and the sizes communicated to the function from the main
function.

After the function call, the main program should print the array with the initial values followed by the
result of the addition as shown below.

Initial values

Scenario 1
312
789

651
345

Result
9 6 3
10 12 14

Scenario 2
31
78

65
34

Result
9 6
10 12

Use as many variables and arrays as needed.

Provide restriction so that the original array passed to it cannot be modified by the function.

 Print using %2d to streamline the output


Q7
For the following problems, create one main program that will
a) create 4 sets of array of characters (one to be used for each function call)
b) populate each array with the same symbols, lowercase and uppercase letters and numbers.
c) print the elements of the array
d) call each of the functions using it’s own array
e) print the expected output (output varies depending on the function)

Functionality of the functions


1. Convert lowercase letters to uppercase letters and print the updated array
2. print the length of the array
3. print the resulting array containing only numbers
4. print the resulting array containing only lowercase letters
Q8
Write a C program that calls a function to add a matrix (two dimensional array).

The main program should ask the user to enter an integer that will be used as the matrix “adder” and
then call the function. The function should perform a matrix increment (every element of the array is
incremented by the same value) and assign the result to another array. The function should be
flexible enough to work with any array size and “adder”, communicated to the function from the main
function.

After the function call, the main program should print the array with the initial values followed by the
added values as shown below.

Use as many variables and arrays as needed. When initializing the arrays, use the initial values shown
for the two arrays in the output.

The program should use both the data set

Provide restriction so that the original array passed to it cannot be modified by the function.

Expected input / output for the two data arrays, if the adder is 2. The adder could be any integer
value.

Enter adder: 2

Data Set 1
Initial values
5 2
1 6
7 3

After adding by 2
7 4
3 8
9 5

Data Set 2

Initial values
5 2 7
1 6 3

After adding by 2
7 4 9
3 8 5
Q9
An array of characters contains a few letters. Write a complete C program that will display the output
as shown below.

Lets assume the array contains =”abcde”. The program should be able to work with any array size,
configurable in the program.

Expected output

Original array = [ a b c d e]

a
ab
abc
abcd
abcde

Do not use global variables.

Note : The interleave function should not accept input or display any output.
The function should work with any size strings.

Caution : Inclusion of unnecessary or unrelated logic or code


segment will result in loss of points.

Write C a program that has a main function and a utility function


called merge. The main function should prompt the user to enter one
short string  (no longer than 20 characters) and hard code another
string with value "123456" (this could be anything, not execeeding
20 characters). It should call the merge function that will interleave
the first string and the second string to produce a third string. The
merge stops with the shortest string. The main program should print
out all the three strings after the function call. 

Use as many variables and arrays as needed. 

Expected input/output: ( do not worry about the exact number of


blank lines in the output)

Scenario 1

Enter string 1 : ABCDE

 
Merged Result

String 1 : ABCDE

String 2 : 123456

Merged string : A1B2C3D4E5

Scenario 2

Enter string 1 : ABCDEFG

Merged Result

String 1 : ABCDEFG

String 2 : 123456

Merged string : A1B2C3D4E5F6

Das könnte Ihnen auch gefallen