Outcome D Examples
Outcome D Examples
C programming
Example 1: FOR loop
Write a C program that calls a function called arraySum. The function arraySum takes two arguments;
an integer array and the number of elements in the array. Have the function return as its result, the
sum of the elements in the array. Use a for loop
Answer
#include <stdio.h>
int arraySum(int arr[10], int num);
int main(void)
{
int val[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
char ch;
#include <stdio.h>
for ( ; ; ) {
prin�("Enter a number: ");
scanf("%d" , &t) ;
if ( t==10 ) break ;
}
#include <stdio.h>
int main(void)
{
int i = 4, j = 4;
do
{
j = i%5;
switch (j--)
{
case 1:
prin�("A");
i *= (++j * 5)*3;
break;
case 2:
prin�("B");
i %= 2;
break;
case 3:
prin�("C");
i -= j;
break;
case 4:
prin�("D");
i %= 12;
break;
case 5:
prin�("E");
i *= 1;
break;
default:
prin�("F");
i *= 1;
break;
}
} while (++i < 10);
return 0;
}
#include <stdio.h>
int main()
int my_array[3][3] =
{40652, 22, 0}
};
prin�("%d\t", my_array[column][row]);
prin�("\n");
prin�("\n");
return 0;
#include <stdio.h>
do
{
prin�("Enter password\n");
scanf("%s", pwd);
} while (strcmp(password, pwd) != 0);
return 0;
}//main
#include <stdio.h>
#include <stdlib.h>
int main()
{
int val[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
char ch[3][3] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
prin�("%d \t", val[i][j]);
}
prin�("\n");
}
prin�("\n\n\n");
i = 0;
while (i < 3) {
j = 0;
while (j < 3) {
prin�("%c \t", ch[i][j]);
j++;
}
i++;
prin�("\n");
}
return 0;
}
Octave
Write an Octave script that calculates the sum of the first N = 10 natural numbers using a for loop.
Expected Output:
The sum of the first 10 natural numbers is 55
Answer
N = 10;
sum = 0;
for i = 1:N
sum = sum + i;
end
Expected Output:
2 4 6 8 10 12 14 16 18 20
Answer
for i = 1:20
if mod(i, 2) == 0
fprin�('%d ', i);
end
end
fprin�('\n');
Use a while loop to find the smallest power of 2 that is greater than 1000.
Expected Output:
Answer
value = 1;
exponent = 0;
Expected Output:
Factorial of 5 is 120
Answer
n = 5;
factorial = 1;
i = 1;
while i <= n
factorial = factorial * i;
i = i + 1;
end
Given a matrix A, use a for loop to calculate the sum of all its elements.
Expected Output:
Answer
A = [1 2 3; 4 5 6; 7 8 9];
[m, n] = size(A);
total = 0;
for i = 1:m
for j = 1:n
total = total + A(i,j);
end
end
Expected Output:
Original Matrix A:
1 2 3
4 5 6
Transposed Matrix A_T:
1 4
2 5
3 6
Answer
A = [1 2 3; 4 5 6];
[m, n] = size(A);
A_T = zeros(n, m); % Pre-allocate transpose matrix
for i = 1:m
for j = 1:n
A_T(j, i) = A(i, j);
end
end
Given a matrix B, use a while loop to find the first negative element and its position. Assume row-
major order (left to right, top to bottom).
Expected Output:
Answer
B = [3 5 7; -1 4 6; 2 -3 8];
[m, n] = size(B);
found = false;
i = 1;
j = 1;
Write a while loop to divide all elements of a matrix C by its maximum value so that all values are
between 0 and 1.
Expected Output:
Normalized Matrix C:
0.1667 0.3333 0.5000
0.6667 0.8333 1.0000
Answer
C = [4 8 12; 16 20 24];
[m, n] = size(C);
max_val = max(C(:));
i = 1;
while i <= m
j = 1;
while j <= n
j = j + 1;
end
i = i + 1;
end
disp('Normalized Matrix C:');
disp(C);
Basic image processing tasks using loops in Octave. These examples assume you're using grayscale
images represented as 2D matrices. Use loops to do things like image inversion, thresholding, and
computing a simple image histogram.
For real image files, Octave’s imread, imshow, and imwrite functions (from the Image package) are
useful. Make sure the Image package is installed and loaded:
Write a script to invert a grayscale image using a for loop. Pixel values range from 0 (black) to 255
(white).
Answer
for i = 1:m
for j = 1:n
inverted_img(i, j) = 255 - img(i, j);
end
end
imshow(inverted_img);
title('Inverted Image');
Effect:
The image is flipped in brightness — black becomes white, white becomes black.
Using a while loop, convert a grayscale image into a binary image where all pixel values above 100
become 255 (white), and others become 0 (black).
Answer
pkg load image;
img = imread('cameraman.tif');
[m, n] = size(img);
binary_img = zeros(m, n, 'uint8');
i = 1;
while i <= m
j = 1;
while j <= n
if img(i, j) > 100
binary_img(i, j) = 255;
else
binary_img(i, j) = 0;
end
j = j + 1;
end
i = i + 1;
end
imshow(binary_img);
title('Binary Threshold Image');
Write a for loop to compute the histogram (pixel intensity count) of a grayscale image.
Answer
pkg load image;
img = imread('cameraman.tif');
histogram = zeros(1, 256); % One bin for each intensity value (0–255)
[m, n] = size(img);
for i = 1:m
for j = 1:n
intensity = img(i, j);
histogram(intensity + 1) = histogram(intensity + 1) + 1; % +1 for Octave indexing
end
end
bar(0:255, histogram);
xlabel('Pixel Intensity');
ylabel('Frequency');
title('Image Histogram');
Write a script to increase the brightness of an image by 50 using a for loop, capping the max value at
255.
Answer
pkg load image;
img = imread('cameraman.tif');
[m, n] = size(img);
bright_img = zeros(m, n, 'uint8');
for i = 1:m
for j = 1:n
new_val = img(i, j) + 50;
bright_img(i, j) = min(new_val, 255);
end
end
imshow(bright_img);
title('Brightened Image');