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
Hi guts gyt edgsahgflefljwegfwygafbhdshdnvcashD
lapo4ka [179]

Hello, how are you and welcome!

4 0
2 years ago
How do I find a space in my String and replace it? JAVA
Fantom [35]

Answer:

This article shows how to use regex to remove spaces in between a String.

A string with spaces in between.

String text = "Hello World Java.";

We want to remove the spaces and display it as below:

Hello World Java.

1. Java regex remove spaces

In Java, we can use regex \\s+ to match whitespace characters, and replaceAll("\\s+", " ") to replace them with a single space.

Regex explanation.

`\\s` # Matches whitespace characters.

+ # One or more

StringRemoveSpaces.java

package com.mkyong.regex.string;

public class StringRemoveSpaces {

public static void main(String[] args) {

String text = "Hello World Java.";

String result = text.replaceAll("\\s+", " ");

System.out.println(result);

}

}

Output

Terminal

Hello World Java.

8 0
2 years ago
Graphic design has evolved rapidly in recent years with the advancement of digital technology. Discuss how the evolution of grap
Yuki888 [10]
We interact with products of graphic design every day. Graphic design is used to make the billboards we drive by on our way to school, the pleasing layout of our favorite website, the covers of our most loved book, even the traffic signs we pass. The evolution of graphic design has only improved our visual world. Designers are constantly seeking to find the best and most effective ways of presentation, which means that the traffic signs you see are in a bold, large font so that you don't have to squint to read them. This is a result of designer's constant process of seeking out the most effective tools of visual communication that will make our lives easier, whether we're aware of it or not.
4 0
3 years ago
im try to search for question the search bar and is giving me a error message is the page having any technical problem?
aalyn [17]
I have been running into the same problem, they haven't said anything but that might be the case.
6 0
2 years ago
class secretType { public: static int count; static int z; secretType(); secretType(int a); void print(); static void incrementY
DanielleElmas [232]

Answer:

The answer to this question can be given as:

In this class definition, there are two constructors.  

Explanation:

In the class definition two constructors are different in type but first we explain constructor that can be as:

Constructor: constructor are special member functions whose task is to initialized as an object of its class.

Rule for defining constructor:

A constructor doesn’t have a return type not even void.

The name of the constructor must be the same as the class name.

A constructor is called automatically when a new instance of an object is created.

Type of constructor:

  1. default constructor.
  2. parameterized constructor.
  3. copy constructor.

In the question there are two type of constructor is used that can be given as:

default constructor:  

The constructor without any parameters is called a default constructor. This type of constructor is called automatically when a new instance of an object is created.

parametrized constructor:

In the parameterized constructor we use at least one parameter in the constructor that is called the parameterized constructor. In the parameterized constructor we can initialize each instance of the class with several values.

Example :

class AB   //define class  

{  

   String name; //define variable

   AB()   //default constructor

   {

   System.out.print("hello...");  //message.

   }

   AB(String name)  //parametrized constructor.

   {  

       this.name = name;  //holding value in name variable.

   }  

}  

class Main  //define class

{  

   public static void main (String[] args)  //main method  

   {  

   AB OB1 =new AB();   //creating class object.

   AB ob2=new AB("XYZ");

   System.out.print(ob2.name);  //print value

   }  

}

Output:

hello... XYZ

5 0
3 years ago
Other questions:
  • Outline a scenario in which you might be acting ethically but might still want to remain anonymous while using the Internet. How
    14·1 answer
  • Write a program that assigns two integer values from standard input to the variables int1 and int2, then prints "true" if they a
    10·1 answer
  • Determine the number of bytes necessary to store an uncompressed RGB color image of size 640 × 480 pixels using 8, 10, 12 and 14
    11·1 answer
  • Can anyone can tell me and explain me the answer of it please .<br>thanks
    9·1 answer
  • You are tasked with accumulating survey data on a web page and are responsible for it being free from dirty data once you close
    11·1 answer
  • CC stand for.....in the email platform?
    12·2 answers
  • कम्प्युटर को पहिलो पुस्ता भनेको के हो?​
    8·2 answers
  • What is the code for forgot password on messenger​
    9·1 answer
  • What are the names of the components (each shown with a leader and a line) of a circuit shown in the diagram?
    7·1 answer
  • What stipulates that the source code of any software published under its license must be freely available.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!