kber
kber

Reputation: 19

Need help solving a problem with an array

Task: Calculate the 25 values of the function y = ax'2 + bx + c on the interval [e, f], save them in the array Y and find the minimum and maximum values in this array.

#include <stdio.h>
#include <math.h>

int main() 
{
    float Y[25];
    int i;
    int x=3,a,b,c;
    double y = a*pow(x,2)+b*x+c;

        printf("a = ", b);
        scanf("%d", &a);

        printf("b = ", a);
        scanf("%d", &b);

        printf("c = ", c);
        scanf("%d", &c);



    for(i=0;i<25;i++)
    {
     printf("%f",y); //output results, not needed
     x++;
    }


    system("pause");
}

Problems:

Upvotes: 0

Views: 256

Answers (3)

Siva Pratheep
Siva Pratheep

Reputation: 96

#include<stdio.h>
#include<math.h>
int main()
{
double y[25];
double x,a,b,c,e,f;
int i,j=0;
printf("Enter a:",&a);
scanf("%lf",&a);
printf("Enter b:",&b);
scanf("%lf",&b);
printf("Enter c:",&c);
scanf("%lf",&c);
printf("Starting Range:",&e);
scanf("%lf",&e);
printf("Ending Range:",&f);
scanf("%lf",&f);
for(i=e;i<=f;i++)
{
    y[j++]=(a*pow(i,2))+(b*i)+c;
}
printf("\nThe Maximum element in the given interval is %lf",y[j-1]);
printf("\nThe Minimum element in the given interval is %lf",y[0]);
}

Good LUCK!

Upvotes: 0

ats
ats

Reputation: 109

Problems:

  • Cant understand how can I use "interval [e,f]" here

(f-e) / 25(interval steps)

  • Cant understand how to save values to array using C libraries

You need to use some form of loop to traverse the array and save the result of your calculation at every interval step. Something like this:

for(int i = 0; i < SIZE; i++) 
// SIZE in this case 25, so you traverse from 0-24 since arrays start 0
  • Cant understand how to write/make a cycle, which will find the minimum and maximum values

For both cases:

traverse the array with some form of loop and check every item e.g. (again) something like this: for(int i = 0; i < SIZE; i++)

For min:

  1. Initialize a double value(key) with the first element of your array
  2. Loop through your array searching for elements smaller than your initial key value.
  3. if your array at position i is smaller than key, save key = array[i];

For max:

  1. Initialize a double value(key) with 0;
  2. Loop through your array searching for elements bigger than your initial key value.
  3. if your array at position i is bigger than key, save key = array[i];

Finally, dont know what exactly i need to do to solve task

  1. Initialize your variables(yourself or through user input)
  2. Create a function that calculates a*x^2 + b*x + c n times for every step of your interval.
  3. Create a function for min & max that loops through your array and returns the smallest/biggest value.

Thats pretty much it. I will refrain from posting code(for now), since this looks like an assignment to me and I am confident that you can write the code with the information @Paul Ogilvie & I have provided yourself. Good Luck

Upvotes: 0

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

You must first ask the user for the values of a, b, c or initialize those variables, and ask for the interval values of e, f, or initialize those variables.

Now you must calculate double interval= (f - e)/25.0 so you have the interval.

Then you must have a loop for (int i=0, double x=e; i<25; i++, x += interval) and calculate each value of the function. You can choose to store the result in an array (declare one at the top) or print them directly.

Upvotes: 1

Related Questions