This collection of programming exercises focuses on fundamental concepts in C, including:
-
Function Definition and Usage: Implementing functions to perform specific tasks, such as calculating the sum and product of integers and the volume of a cube.
-
Array Manipulation: Working with one-dimensional arrays, initializing elements, accessing values, and performing operations like summing elements.
-
Input Handling: Reading user input using
scanfand processing that input to compute results, enhancing interaction with the user. -
Control Structures: Utilizing loops for iterating over arrays and displaying results, reinforcing understanding of control flow in programming.
-
String Processing: Managing strings by declaring character arrays and displaying individual characters, emphasizing basic string manipulation techniques.
Through these exercises, I have strengthened my problem-solving skills and gained practical experience in applying C programming concepts to real-world scenarios.
Write a function g() that computes the sum and product of two integers. The sum is stored as a global variable, and the product is returned by the function.
#include <stdio.h>
int g(int, int);
int sum;
int main(void)
{
int product = g(3, 4);
printf("Sum=%d\n", sum);
printf("Product=%d\n", product);
}
int g(int x, int y)
{
sum = x + y; // Calculate sum
return x * y; // Return product
}Write a function cube_volume that calculates the volume of a cube given its side length.
float cube_volume(float side)
{
return side * side * side; // Calculate volume
}
### Part B: Function Call in main()
Provide the code to invoke the function and display the result.
### Code
int main(void)
{
float side = 3.0;
float volume = cube_volume(side);
printf("Volume of the cube: %.2f\n", volume);
}void f(int a[])
{
printf("%d\n", a[3]);
a[3] = a[3] + 900;
}
int main(void)
{
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
(void) f(a);
(void) f(a);
printf("%d\n", a[3]);
return 0;
}- The first call to
f(a)prints3, then adds900toa[3], making it903. - The second call to
f(a)prints903and adds900again, making it1803. - Finally, the program prints
1803.
Declare, initialize, and manipulate a 1-D array of integers.
#include <stdio.h>
int main(void)
{
int a[12] = {1, 0, 3, 0, 0, 0, 7, 0, 0, 0, 0, 0};
// Display array contents
for (int i = 0; i < 12; i++)
{
printf("%d\n", a[i]);
}
// Sum the array elements
int sum = 0;
for (int i = 0; i < 12; i++)
{
sum += a[i];
}
printf("Sum of array elements: %d\n", sum);
return 0;
}Declare a float array, read user input, and calculate the sum and average.
#include <stdio.h>
int main(void)
{
float numbers[5];
float sum = 0.0;
// Read numbers from console
for (int i = 0; i < 5; i++)
{
printf("Enter number %d: ", i + 1);
scanf("%f", &numbers[i]);
}
// Calculate sum and average
for (int i = 0; i < 5; i++)
{
sum += numbers[i];
}
float average = sum / 5;
printf("Sum: %.2f\n", sum);
printf("Average: %.2f\n", average);
return 0;
}Declare a string and display its characters one at a time.
#include <stdio.h>
int main(void)
{
char str[] = "UNC Charlotte";
// Display each character on a new line
for (int i = 0; str[i] != '\0'; i++)
{
printf("%c\n", str[i]);
}
return 0;
}These exercises reinforce C programming fundamentals through function definitions, array manipulations, user input handling, and string processing.