A blog posting problems and tutorials for blog visitors to practice and gain experience.
AD1
Friday, 22 December 2017
Saturday, 23 September 2017
Monday, 18 September 2017
Matrix Transpose
Given a 2D array A, your task is to convert all rows to columns and columns to rows.
Input:
First line contains 2 space separated integers, N - total rows, M - total columns.
Each of the next N lines will contain M space separated integers.
Output:
Print M lines each containing N space separated integers.
Constraints:
1≤N≤10
1≤M≤10
0≤A[i][j]≤100 where 1≤i≤N and 1≤j≤M
Solution
Input:
First line contains 2 space separated integers, N - total rows, M - total columns.
Each of the next N lines will contain M space separated integers.
Output:
Print M lines each containing N space separated integers.
Constraints:
1≤N≤10
1≤M≤10
0≤A[i][j]≤100 where 1≤i≤N and 1≤j≤M
Solution
Saturday, 16 September 2017
Count frequency of each element in array
Write a program to count the frequency of each element in array , as example if we have
int a[10]={5, 10, 2, 5, 50, 5, 10, 1, 2, 2};
the output should be
number frequency
5 3
10 2
2 3
50 1
1 1
int a[10]={5, 10, 2, 5, 50, 5, 10, 1, 2, 2};
the output should be
number frequency
5 3
10 2
2 3
50 1
1 1
Tuesday, 22 August 2017
Friday, 18 August 2017
Friday, 14 July 2017
Wednesday, 12 July 2017
Tuesday, 11 July 2017
Saturday, 8 July 2017
Friday, 7 July 2017
Saturday, 17 June 2017
C program to left rotate an array
Write a program to left rotate an array by n position
Example
Input
Input 10 elements in array: 1 2 3 4 5 6 7 8 9 10 Input number of times to rotate: 3
Output
Array after left rotation: 4 5 6 7 8 9 10 1 2 3Solution
Count Set bits in an array of integers
assume you have an array of 5 integers and you want to count the numbers of set bits in all array elements , write a function to do that
Solution
Solution
Circular Bit Shift
Write two functions RL(data,n) which performs circular left shift on variable data by n bits , RR(data,n) which performs circular right shift on variable data by n bits
note: data is 1 byte variable
refer to this video to know more about circular bit shifts
Solution
note: data is 1 byte variable
refer to this video to know more about circular bit shifts
Solution
Friday, 16 June 2017
Saturday, 3 June 2017
Saturday, 27 May 2017
Reverse Two Arrays Of The Same Size
write a C program to reverse two array elements of the same size
as example if we have int a[]={1,2,3,4},int b[]={5,6,7,8}
after reversing we would have int a[]={5,6,7,8},int b[]={1,2,3,4}
as example if we have int a[]={1,2,3,4},int b[]={5,6,7,8}
after reversing we would have int a[]={5,6,7,8},int b[]={1,2,3,4}
Monday, 1 May 2017
Saturday, 29 April 2017
Find the output of the following code
Answer
2147483647
Explanation
i is an uninitialized static variable so its default value is zero
also i automatically promoted to unsigned int (because we did not specify if its signed or unsigned)
then 0<=10 condition true
inside while loop 0>2 false , i-- executed
second iteration i becomes 2147483647 (int size on my machine is 4 bytes)
2147483647 <=10 false , while loop is bypassed and 2147483647 is printed.
Find the output of the following code
Answer
i
Explanation
char c=++*p++
this statement will be parsed as follow
1)p++ p which is a char pointer is post incremented so p still points to 'h'
2)*p++ p is dereferenced to get value 'h'
3)++'h' is i
note:this result is highly compiler dependent , some compilers may fail to compile this code and others may get you this output
advise : never write a code which seems to be ambiguous and compiler dependent
Tuesday, 25 April 2017
Find the output of the following code
Output
20
C is sea
Explanation
in this question I introduce the concept of preprocessor operators
concat(x,y) x##y this operator concatenate x and y
string(a) #a #operator convert any macro constant into string
so after preprocessing this code you will have the following code
int myvar=20;
printf("%d\n",myvar);
printf("%s","C is sea");
20
C is sea
Explanation
in this question I introduce the concept of preprocessor operators
concat(x,y) x##y this operator concatenate x and y
string(a) #a #operator convert any macro constant into string
so after preprocessing this code you will have the following code
int myvar=20;
printf("%d\n",myvar);
printf("%s","C is sea");
Find the output of the following code
Output
lvalue required as increment operand
Explanation
after reading the code for the first time you may get tricked and say the output will be
c
c++
python
java
but this not the output , because you will get compiler error in line 6 when you try to modify the base address of an array which is already a constant pointer
Find the output of the following code
Output
linker error (undefined symbol x)
Explanation
extern keyword tells the compiler that the value of the variable x is not in this scope so the compiler does not know variable x memory location so he will not assign x memory location , otherwise during linking time if x is defined in another scope or file , its value will be printed
Find the output of the following code
Output
h h
e e
l l
l l
o o
Explanation
message[i]=i[message]
because compilers convert message[i] into *(message+i)
where message is the array base address ,also i[message] is converted into
*(i+message)
so the two expressions are the same
Tuesday, 4 April 2017
Big endian VS little endian
Write a C program to check if your processor is big endian or little endian
solution
solution
Tuesday, 21 March 2017
Monday, 13 March 2017
Reverse bits of Byte
write a C function to reverse the bits of a Byte as example if we have variable unsigned char x=0x07=0b00000111 as input parameter after reversing its bits it would return 11100000
solution
Tuesday, 7 March 2017
Palindrome String
Write a C code to check a String is palindrome or not , Some palindrome strings examples are "a", dad", "radar", "madam", "abcba" etc.
solution
Monday, 6 March 2017
From decimal to binary
Write a C program to convert an integer from decimal to binary , using logical bitwise operators
solution
solution
Thursday, 2 March 2017
what is the output
what is the output of this C code
output:
66 B
4
Explanation:
union temp t={66,'A'};
this statement will initialize only the first member of the union x with 66 and the compiler will ignore the second value 'A', as one element is sufficient to initialize all union members as they share the same memory locations
sizeof(t) =4 size of largest element in memory location
output:
66 B
4
Explanation:
union temp t={66,'A'};
this statement will initialize only the first member of the union x with 66 and the compiler will ignore the second value 'A', as one element is sufficient to initialize all union members as they share the same memory locations
sizeof(t) =4 size of largest element in memory location
Wednesday, 1 March 2017
what is the output of this C code
output :
compilation error in line 19 , as you can not assign two structures of different types
also there is a compilation error in line 21 as rational operators can not be applied on structures
compilation error in line 19 , as you can not assign two structures of different types
also there is a compilation error in line 21 as rational operators can not be applied on structures
Tuesday, 28 February 2017
compute occurrence of character in a string
write C Program print the frequency of a certain character in a string
Wednesday, 22 February 2017
find the output
what will be the output of the following code
output will be
8 8 8 4
size of a pointer to any data type is type independent , size of pointer depends on your machine address bus.
sizeof("clu") here "clu" is treated as array of characters ={'c','l','u','\0'}
so sizeof("clu") is 4 bytes
output will be
8 8 8 4
size of a pointer to any data type is type independent , size of pointer depends on your machine address bus.
sizeof("clu") here "clu" is treated as array of characters ={'c','l','u','\0'}
so sizeof("clu") is 4 bytes
Tuesday, 14 February 2017
Saturday, 11 February 2017
Number of words in a string
Write a C function words(string) that accepts a string as input parameter and returns the number of words in this string
as example
words("my name is mohamed") will return 4
as example
words("my name is mohamed") will return 4
Friday, 10 February 2017
Sunday, 5 February 2017
Even or Odd
Write a C program to check if any entered number is even or odd without the use of conditional statements (if,else)
No loops
Write a C program to print the numbers from 1 to 10 without using any looping statements (for/while/do while)
also do not write printf() 10 times
also do not write printf() 10 times
Thursday, 2 February 2017
convert to uppercase
Write a function upper() that accepts a string and converts all its characters into uppercase characters
as example upper("c programming")=C PROGRAMMING
as example upper("c programming")=C PROGRAMMING
reverse a string
Write a function reverse() that accepts a string and reverses it
as example reverse("hello") would return "olleh"
string of numbers
write a function that accepts string of numbers and converts it to digits
as example convert("123")=123
as example convert("123")=123
Remove all blank spaces in a string
Write a C program to remove all the blank spaces from a string.
as example if string is "hello world , C programming" then the output will be "helloworld,Cprogramming"
Wednesday, 25 January 2017
Tuesday, 24 January 2017
Pointers and bitfields
what will be the output of the following code
the code creates compilation error , as you can not create a pointer to bit fields , simply because bit fields may not start at byte boundryThursday, 19 January 2017
Swap structure values
Assume that you have a structure defined as follow
struct two_integers {int n1;int n2;};
and this structure is initialized as follow
struct two_integers x={100,-100};
Write a C function that returns the same structure x but n1 and n2 are swapped
struct two_integers {int n1;int n2;};
and this structure is initialized as follow
struct two_integers x={100,-100};
Write a C function that returns the same structure x but n1 and n2 are swapped
Structures
In the following code , assume that the base address of the structure is 2424384 (in decimal form) , what will be the output of the program
Solution
2424384
2424384
2424388
2424392
Solution
2424384
2424384
2424388
2424392
Tuesday, 17 January 2017
Sorting 2D array
Assume you have 2D array of integers , int a[3][4]={8,7,6,5,
-2,800,19,-40,
1,-23,600,-500};
Write a C program to sort it in ascending order (from low to high)
Solution
-2,800,19,-40,
1,-23,600,-500};
Write a C program to sort it in ascending order (from low to high)
Solution
Monday, 16 January 2017
Increment all array values using pointers
Write C function INC(int array[5],int n) that accepts two parameters array of five integers and integer, and adds the integer n to all array elements , then it returns the new array , as example if array[5]={1,5,7,0,2} and n =5 after function call we have array[]={6,10,12,5,7}
Saturday, 14 January 2017
Pointers and 2D arrays
Assume you have 2D array S[2][4]={{1234,56},
{1212,33},
{1434,80},
{1312,78}};
write a C program to print all the values and their corresponding addresses of this array
using pointers
{1212,33},
{1434,80},
{1312,78}};
write a C program to print all the values and their corresponding addresses of this array
using pointers
size of double data type
Write a program to calculate the size of a double data type in terms of Bytes , Do not use the sizeof() operator
Solution
Wednesday, 11 January 2017
Tuesday, 10 January 2017
From C file to exe file
Describe in detail how a C program is compiled and executed
Answer
1) The preprocessor code searches for preprocessor directives (mainly #include or #define) and substitute them with their codes,the output of this step creates an expanded file (.I file)
2) the compiler takes .I file and coverts the high level code into assembly then into machine language (binary format) and generate .obj file
3) the linker takes .obj file and link it with C standard library functions in your code
and generates the executable version of your C code
Monday, 9 January 2017
Wednesday, 4 January 2017
Subscribe to:
Posts (Atom)