0% found this document useful (0 votes)
6 views12 pages

Day7 Assignment

The document contains a series of programming assignments focused on the use of constant variables in C. Each assignment teaches a specific concept related to constants, such as declaring constant variables, using const with pointers, and creating constant expressions. The assignments include code examples and objectives to help learners understand the importance and application of constants in programming.

Uploaded by

bettinakpeter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views12 pages

Day7 Assignment

The document contains a series of programming assignments focused on the use of constant variables in C. Each assignment teaches a specific concept related to constants, such as declaring constant variables, using const with pointers, and creating constant expressions. The assignments include code examples and objectives to help learners understand the importance and application of constants in programming.

Uploaded by

bettinakpeter
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

/*Assignment 1: Constant Variable Declaration


Objective: Learn to declare and initialize constant variables.
Write a program that declares a constant integer variable for the value of Pi
(3.14) and prints it. Ensure that any attempt to modify this variable results in
a compile-time error. */

#include <stdio.h>
float const Pi = 3.14;
int main() {
printf("Value of Pi: %.2f\n", Pi);
// Pi = 3.15; // Attempt to modify the constant variable
return 0;
}

2.
/*Assignment 2: Using const with Pointers
Objective: Understand how to use const with pointers to prevent modification of
pointed values.
Create a program that uses a pointer to a constant integer. Attempt to modify the
value through the pointer and observe the compiler's response. */

#include <stdio.h>
int main()
{
int a = 10;
int const *pData = (int*) &a;

printf("Original value of a: %d\n", *pData);

//*pData = 0; // error: assignment of read-only location '*pData'

// printf("Modified value of a through pointer: %d\n", a);

return 0;
}
3.
/*Assignment 3: Constant Pointer
Objective: Learn about constant pointers and their usage.
Write a program that declares a constant pointer to an integer and demonstrates
that you cannot change the address stored in the pointer. */

#include <stdio.h>
#include <stdint.h>
int main() {
int a = 10;
uint8_t *const pData = (uint8_t*)&a;
printf("Address of a: %p\n", pData);

// int b;
// pData = (int*)&b; //error: assignment of read-only variable 'pData'
// printf("Address of b after changing the pointer: %d\n", pData);

*pData = a + 10; // ponted value can be updated but not the address stored in
the pointer
printf("Value after a updation: %d\n", a);

return 0;
}

4.
/*Assignment 4: Constant Pointer to Constant Value
Objective: Combine both constant pointers and constant values.
Create a program that declares a constant pointer to a constant integer.
Demonstrate that neither the pointer nor the value it points to can be changed.
*/

#include <stdio.h>
#include <stdint.h>
int main(){
int a = 20;
printf("original value of a = %d\n", a);
uint8_t const* const pData = (uint8_t*)&a;
printf("original pointer value = %p\n", pData);

// Attempt to modify the value


// *pData = 50; // error message
// printf("modified value of a = %d\n", a);

// Attempt to modify the pointer


// int b;
// pData = &b; // error message
// printf("modified pointer value = %p\n", pData);

return 0;
}

5.
/*Assignment 5: Using const in Function Parameters
Objective: Understand how to use const with function parameters.
Write a function that takes a constant integer as an argument and prints its
value. Attempting to modify this parameter inside the function should result in
an error. */

#include <stdio.h>
int func_print(int const num) {
printf("Given value of integer: %d\n", num);
// num = num + 1; // error: assignment of read-only parameter 'num'
// printf("Modified value of integer: %d\n", num);
}

int main() {
int num = 10;
func_print(num);
return 0;
}
6.
/*Assignment 6: Array of Constants
Objective: Learn how to declare and use arrays with const.
Create an array of constants representing days of the week. Print each day using
a loop, ensuring that no modifications can be made to the array elements. */

#include <stdio.h>

int main()
{
char const *days_of_week[] ={"sunday", "monday", "tuesday", "wednesday",
"thursday", "friday", "saturday"};

for (int i = 0; i < 7; i++) {


printf("%s\n", days_of_week[i]);
// *days_of_week[i] = "hello"; // error: assignment of read-only location
'*days_of_week[i]'
}

return 0;
}

7.
/*Assignment 7: Constant Expressions
Objective: Understand how constants can be used in expressions.
Write a program that uses constants in calculations, such as calculating the area
of a circle using const.*/

#include <stdio.h>
int main()
{
float radius;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);

float const area = 3.14159 * radius * radius;


printf("The area of the circle is: %.2f\n", area);

// area = area * 4; // error: assignment of read-only variable 'area'


// printf("The area of the circle if radius is doubled: %.2f\n", area);

return 0;
}

8.
/*Assignment 8: Constant Variables in Loops
Objective: Learn how constants can be used within loops for fixed iterations.
Create a program that uses a constant variable to define the number of iterations
in a loop, ensuring it cannot be modified during execution. */

#include <stdio.h>

int main() {

int const n = 10;

for (int i = 0; i < n; i++) {


printf("Iteration %d\n", i + 1);
// n = i + 1; // error: assignment of read-only variable 'n'
}

printf("After the loop, the value of n is still: %d\n", n);

return 0;
}
9.
/*Assignment 9: Constant Global Variables
Objective: Explore global constants and their accessibility across functions.
Write a program that declares a global constant variable and accesses it from
multiple functions without modifying its value. */

#include <stdio.h>

const float pi = 3.14159;

float area(int radius) {


return pi * radius * radius;
}

float circumference(int radius) {


return 2 * pi * radius;
}

int main() {
int radius = 5;
printf("Area: %.2f\n", area(radius));
printf("Circumference: %.2f\n", circumference(radius));
return 0;
}
10.
/* program to find all the prime numbers from 3-100
Requirements:
no input to the program
the output will be each prime number separated by a space on a single line
create an array that will store ech prime number as it is generated
hard-code the first two prime numbers (2 and 3) in the primes array
utilize loops only to find prime numbers up to 100 and a loop to print out the
primes array*/

#include<stdio.h>
#include<math.h>
int main(){
int flag = 2;
int array[100] = {2, 3};
for(int i=4; i<=100; i++){
int isPrime = 1;
for(int j=2; j<= sqrt(i); j++){
if(i%j==0){
isPrime = 0;
break;
}
}
if (isPrime) {
array[flag] = i;
flag++;
}
}

for(int i=0; i< flag; i++){


printf("%d ", array[i]);
}

return 0;
}

11.
// Create a program that reverses the elements of an array. Prompt the user to
enter values and print both the original and reversed arrays.

#include <stdio.h>
int main(){
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr[n];
printf("Enter the elements of the array:\n");
for(int i = 0; i < n; i++){
scanf("%d", &arr[i]);
}

printf("Original array: ");


for(int i = 0; i < n; i++){
printf("%d ", arr[i]);
}

printf("\nReversed array: ");


for(int i = n-1; i >= 0; i--){
printf("%d ", arr[i]);
}

return 0;
}

12.
//Write a program that to find the maximum element in an array of integers. The
program should prompt the user for input and display the maximum value.
#include<stdio.h>
int main() {
int n, max;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int array[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &array[i]);
}

max = array[0];
for (int i = 1; i < n; i++) {
if( array[i] > max )
{
max = array[i];
}
}

printf("Maximum element in the array is: %d\n", max);

return 0;
}

13.
//Write a program that counts and displays how many times a specific integer
appears in an array entered by the user.

#include <stdio.h>

int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int array[n];
int count[n];

printf("Enter the elements of the array:\n");


for (int i = 0; i < n; i++) {
scanf("%d", &array[i]);
count[i] = 0;
}

for (int i = 0; i < n; i++) {


if (count[i]) {
continue;
}

int counter = 1;
for (int j = i+1; j < n; j++) {
if (array[j] == array[i]) {
counter++;
count[j] = 1;
}
}

printf("Element %d appears %d times.\n", array[i], counter);


}
return 0;
}

14.
/*Create a program that uses two-dimensional array in weather program.
This program will find the total rainfall for each year, the average yearly
rainfall, and the average rainfall for each month.
Input will be a 2D array with hard-corded values for rainfall amounts for the
past 5 years.
The array should have 5 rows and 12 columns.
Rainfall amounts can be floating point numbers. */

#include<stdio.h>
int main() {
float total_yearly_rainfall[5] = {0.0},total_rainfall = 0.0,
average_yearly_rainfall = 0.0, average_monthly_rainfall[12] = {0.0};
float rainfall[5][12] = {
{3.36, 3.08, 3.63, 4.33, 4.86, 5.52, 3.77, 3.49, 2.97, 3.87, 3.95, 4.16},
{3.55, 3.17, 3.53, 4.54, 4.76, 5.14, 3.56, 3.35, 2.77, 3.74, 4.04, 4.28},
{3.10, 3.23, 3.48, 4.15, 4.70, 5.31, 3.35, 3.18, 2.58, 3.58, 3.91, 4.11},
{3.50, 3.02, 3.56, 4.42, 4.95, 5.45, 3.64, 3.45, 2.85, 3.85, 4.00, 4.33},
{3.28, 3.20, 3.46, 4.29, 4.80, 5.30, 3.67, 3.34, 2.93, 3.70, 3.89, 4.08}
};

for (int i = 0; i < 5; i++) {


for (int j = 0; j < 12; j++) {
total_yearly_rainfall[i] += rainfall[i][j];
}
}

for (int i = 0; i < 5; i++) {


total_rainfall += total_yearly_rainfall[i];
}

average_yearly_rainfall = total_rainfall / 5;

for (int i = 0; i < 12; i++) {


for (int j = 0; j < 5; j++) {
average_monthly_rainfall[i] += rainfall[j][i];
}
average_monthly_rainfall[i] /= 5;
}

int year = 2010;


printf("YEAR \t RAINFALL (inches)");
for (int i = 0; i < 5; i++) {
printf("\n%d \t %.1f", year, total_yearly_rainfall[i]);
year++;
}

printf("\n\nThe yearly average is %.1f inches.", average_yearly_rainfall);


printf("\n\nMONTHLY AVERAGES:\n");
printf("\nJan \tFeb \tMar \tApr \tMay \tJun \tJul \tAug \tSep \tOct \tNov
\tDec\n");

for (int i = 0; i < 12; i++) {


printf("%.1f\t", average_monthly_rainfall[i]);
}

return 0;

You might also like