1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Soloha48 [4]
3 years ago
10

We will use linear interpolation in a C program to estimate the population of NJ between the years of the census, which takes pl

ace every 10 years. For our program the x's are the years and the y's are the population. So given a year, we will use linear interpolation to calculate the population for that year, using the surrounding census years.
1. read the data from the file njpopulation.dat into two arrays, one for the years and one for the populations
2. use int for the years and float for the populations
3. create a loop to do the following, until the user enters 0 1. prompt the user to enter a year between 1790 and 2010 2. give an error message and reprompt if the user enters a value outside the valid range 3. use linear interpolation to approximate the population for the given year
4. print the year and the approximate population; print the population with two decimal places
Computers and Technology
1 answer:
nikklg [1K]3 years ago
5 0

Answer:

See explaination for program code

Explanation:

program code below:

#include<stdio.h>

int main()

{

//file pointer to read from the file

FILE *fptr;

//array to store the years

int years[50];

//array to store the population

float population[50];

int n = 0, j;

//variables for the linear interpolation formula

float x0,y0,x1,y1,xp,yp;

//opening the file

fptr = fopen("njpopulation.dat", "r");

if (fptr != NULL)

{

//reading data for the file

while(fscanf(fptr, "%d %f", &years[n], &population[n]) == 2)

n++;

//prompting the user

int year = -1;

printf("Enter the years for which the population is to be estimated. Enter 0 to stop.\n\n");

while(year != 0)

{

printf("Enter the year (1790 - 2010) : ");

scanf("%d", &year);

if (year >= 1790 && year <= 2010)

{

//calculating the estimation

xp = year;

for (j = 0; j < n; j++)

{

if (year == years[j])

{

//if the year is already in the table no nedd for calculation

yp = population[j];

}

else if (j != n - 1)

{

//finding the surrounding census years

if (year > years[j] && year < years[j + 1])

{

//point one

x0 = years[j];

y0 = population[j];

//point two

x1 = years[j + 1];

y1 = population[j + 1];

//interpolation formula

yp = y0 + ((y1-y0)/(x1-x0)) * (xp - x0);

}

}

}

printf("\nEstimated population for year %d : %.2f\n\n", year, yp);

}

else if (year != 0)

{

printf("\nInvalid chioce!!\n\n");

}

}

printf("\nAborting!!");

}

else

{

printf("File cannot be opened!!");

}

close(fptr);

return 0;

}

OUTPUT :

Enter the years for which the population is to be estimated. Enter 0 to stop.

Enter the year (1790 - 2010) : 1790

Estimated population for year 1790 : 184139.00

Enter the year (1790 - 2010) : 1855

Estimated population for year 1855 : 580795.00

Enter the year (1790 - 2010) : 2010

Estimated population for year 2010 : 8791894.00

Enter the year (1790 - 2010) : 4545

Invalid chioce!!

Enter the year (1790 - 2010) : 1992

Estimated population for year 1992 : 7867020.50

Enter the year (1790 - 2010) : 0

Aborting!!

You might be interested in
What does it mean by MYRIAD of data?
pentagon [3]

Answer:

The term Myriad means an indefinitely large number of things. And when we say Myriad of data, we mean an indefinitely large amount of data like in Petabytes.

Explanation:

What it does implies is that you are dealing with a large amount of data. And hence we are dealing with something like bigData. And hence, technologies related to BigData are going to operate. And this term is used as a noun as well as an adjective when we talk about English. And hence, you can use it with "a" or without the "a" as well. It has been a part of reputable English and has been repeatedly used by top authors like Milton.

5 0
3 years ago
Software development team leaders need blank skills to excel in directing their teams
IrinaK [193]
I mean, this is tricky because there's a lot of skills they'd need - c<span>ommunication, leadership, organisation, confidence etc. Is there a list of answers to choose from?</span>
5 0
3 years ago
Read 2 more answers
Which is an example of artificial intelligence in computers? A. multimedia software B. encryption software C. voice recognition
andrezito [222]

Answer:

its option b encryption software

Explanation:

Artificial intelligence (AI) is intelligence demonstrated by machines, unlike the natural intelligence displayed by humans and animals, which involves consciousness and emotionality. The distinction between the former and the latter categories is often revealed by the acronym chosen. 'Strong' AI is usually labelled as AGI (Artificial General Intelligence) while attempts to emulate 'natural' intelligence have been called ABI (Artificial Biological Intelligence). Leading AI textbooks define the field as the study of "intelligent agents": any device that perceives its environment and takes actions that maximize its chance of successfully achieving its goals.[3] Colloquially, the term "artificial intelligence" is often used to describe machines (or computers) that mimic "cognitive" functions that humans associate with the human mind, such as "learning" and "problem solving".

3 0
2 years ago
Which of the following blocks is least similar to the others?
GuDViN [60]
The answer is A or B
4 0
3 years ago
Read 2 more answers
What happens to a mechanical system when two opposing torques do not cancel out?
const2013 [10]
The system will move in the direction of the stronger torque
5 0
3 years ago
Other questions:
  • The magnavox odyssey was a commercial success
    13·1 answer
  • To help determine which technology to use, Raul creates a table to help him compare the pros and cons of his choices. What part
    15·1 answer
  • PLEASE HELP POWER POINT:
    8·2 answers
  • William has an internet connection that does not allow him to make calls when connected to the internet. what internet service c
    7·2 answers
  • Program documentation _____.
    8·1 answer
  • in java how do i Write a program that reads a set of integers, and then prints the sum of the even and odd integers.
    6·1 answer
  • Modify the NumberedList class we implementd during the lecture by adding a member function: void NumberedList::insertPosition(in
    13·1 answer
  • when files on storage are scattered throughout different disks or different parts of a disk, what is it called
    10·1 answer
  • Could anyone please answer this?
    15·1 answer
  • What is software engineering? What are the objectives of software engineering?
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!