0% found this document useful (0 votes)
4 views7 pages

Assignment 1

The document outlines several programming assignments in C, focusing on floating-point precision issues, specifically the representation of 0.1 in binary. It includes practical exercises that demonstrate the accumulation of 0.1 over different iterations, simulate time drift in a missile system, and a radar tracking simulation to show prediction errors. Each practical includes code snippets and expected outputs to illustrate the concepts being taught.

Uploaded by

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

Assignment 1

The document outlines several programming assignments in C, focusing on floating-point precision issues, specifically the representation of 0.1 in binary. It includes practical exercises that demonstrate the accumulation of 0.1 over different iterations, simulate time drift in a missile system, and a radar tracking simulation to show prediction errors. Each practical includes code snippets and expected outputs to illustrate the concepts being taught.

Uploaded by

raghavsood876
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Assignment 1

Practical 1

Objective- Write a program in C that 0.1 cannot be represented exactly in binary


floating point.

#include <stdio.h>

int main() {

float a = 0.1;

printf("Value of a stored in float: %.20f\n", a);

if (a == 0.1)

printf("a is equal to 0.1\n");

else

printf("a is NOT exactly equal to 0.1\n");

return 0;

}
Practical 2

Objective- Write a program in C to add 0.1 repeatedly (10 times, 1000 times, 1 million
times) and compare expected vs actual

#include <stdio.h>

int main() {

float sum;

int i;

// Add 0.1 ten times

sum = 0.0;

for(i = 0; i < 10; i++) {

sum = sum + 0.1;

printf("Adding 0.1 ten times:\n");

printf("Expected value = 1.0\n");

printf("Actual value = %.20f\n\n", sum);

// Add 0.1 one thousand times

sum = 0.0;

for(i = 0; i < 1000; i++) {

sum = sum + 0.1;

printf("Adding 0.1 one thousand times:\n");

printf("Expected value = 100.0\n");

printf("Actual value = %.20f\n\n", sum);

// Add 0.1 one million times


sum = 0.0;

for(i = 0; i < 1000000; i++) {

sum = sum + 0.1;

printf("Adding 0.1 one million times:\n");

printf("Expected value = 100000.0\n");

printf("Actual value = %.20f\n", sum);

return 0;

Practical 3
Simulate Patriot-style time drift. Method:

● Consider system tick = 0.1 sec

● Convert ticks into time using approximation

● Run for large duration

● Calculate total drift after 100 hours Deliverable for students:

● Program prints:

● total ticks in 100 hours

● estimated drift

● equivalent distance error for missile speed

#include <stdio.h>

int main() {

double tick = 0.1;

int ticks_per_second = 10;

int hours = 100;

double total_seconds = hours * 3600.0;

long long total_ticks = total_seconds * ticks_per_second;

// approximate time calculation

double estimated_time = total_ticks * tick;

// expected time

double actual_time = total_seconds;

double drift = estimated_time - actual_time;

double missile_speed = 1676.0; // m/s


double distance_error = missile_speed * drift;

printf("Total ticks in 100 hours: %lld\n", total_ticks);

printf("Expected time (seconds): %.10f\n", actual_time);

printf("Estimated time (seconds): %.10f\n", estimated_time);

printf("Total drift (seconds): %.10f\n", drift);

printf("Distance error (meters): %.10f\n", distance_error);

return 0;

}
Practical 4: Range-Gate Failure Simulation

Aim

Demonstrate how prediction error causes the radar to miss a fast target when the
predicted position differs too much from the actual position.

#include <stdio.h>

int main() {

double target_speed = 1676.0; // m/s

double real_time = 100.0; // seconds

double drift = 0.01; // timing error

double predicted_time = real_time + drift;

double actual_position = target_speed * real_time;

double predicted_position = target_speed * predicted_time;

double error = predicted_position - actual_position;

double range_gate = 50.0; // threshold distance

printf("Actual position: %.2f meters\n", actual_position);

printf("Predicted position: %.2f meters\n", predicted_position);

printf("Prediction error: %.2f meters\n", error);

if(error > range_gate)

printf("Tracking failed: Target missed\n");

else

printf("Tracking successful\n");

return 0;
}

PROJECT 1: Smart Scientific Calculator

You might also like