-1

f(t)={t+3: t ∈[0,T].
0: otherwise}

  1. Read in an integer value for T. Sample the function f(t) above, at t = 0, 1, 2 . . . T and write the values to a file ‘function.txt’.

  2. Read the values from the file, and place them in an array.

  3. Write a function called “convolution” which takes two arrays as arguments, calculates their discrete convolution and prints out the resulting function in two columns: index and convolution. Print the output both to the screen and to a file called ‘convolution.txt’. Your function should only do the calculation for indices corresponding to non-zero values of the convolution. Assume both functions are only nonzero between 0 and T. (hint: the convolution will only be nonzero between 0 and 2T- make sure you understand why.)

  4. Call your function with both functions f and g given by the same array from part 1. The formula for finding the convolution is

    (f*g)[n] ∑ (f[m] g[n-m])

This is what I have so far. need help finishing it.

#include <stdlib.h>
#include <stdio.h>
void convolution (int F[ ] ,int G [ ])
{

    FILE *fptr;
    int i,j,T;
    int x;
    F[T],G[T];
    x= T+1;
    fptr = fopen("function.txt","r");
    if (fptr == NULL)
    {
        printf("No such file!");
        exit(0);
    }
    for (i=0; i<=x; i++)
    {
        F[i]= i+3;
        fscanf(fptr,"%d",&j);
        printf("F[%d] = %d\n",i,F[i]);
    }
    printf("\n");
    fclose(fptr);
    int sum;
    for (c=0; c<2T+1; c++)
    {
       sum =0; g=c;
       for(f=0; f<c; f++,g--)
       {
            if (f<=T && g<=T)
            {
                 sum = sum + F [ ]* G [ ];
            }
            printf("convolution [%d] = %d",c, sum);
       }
    }
}

int main (void)
{
    int x;
    int T,F[x],G[x];
    x= T+1;
    printf("Enter the value of T: ");
    scanf("%d",&T);
    convolution(F,G);
    return 0;
 }
Harshith Rai
  • 3,018
  • 7
  • 22
  • 35
Aw Hu
  • 1
  • What is `F[T],G[T];` supposed to do? Even if `T` was initialized before which it is not. – Gerhardh Nov 23 '18 at 07:53
  • 1
    Wonder how many warnings you have to ignore to get this code to compile. If you are using gcc/clang add `-Wall -Wextra -pedantic` compiler options to enable warnings, and if you are using VS, then add `/W3`. – David C. Rankin Nov 23 '18 at 07:56

2 Answers2

0

There are many uninitialized variables in your code. Reading them before writing into them causes undefined behavior.

For example, in main the variable x is uninitialized. You are using it for defining variable length arrays F[x] and G[x].

In the convolution function the vairiable T is uninitialized. And you are using its value in the statement x= T+1; This is undefined behavior. See this question and its answers for the reason.

And even if the initial value of T happens to be 0, then the inner for loop will not be reached as the value of c cannot exceed 1.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • In `main`, it is being used before being initialized. – P.W Nov 23 '18 at 07:58
  • Yes, and also in `main` it is already used before the statement you mention. It is used to define VLA of "random" length – Gerhardh Nov 23 '18 at 08:03
  • In main, its `F[x],G[x]`, not `F[T], G[T]`. So T is not used there. In any case, what if T or x contains a negative value. What will happen to array declarations then? – P.W Nov 23 '18 at 08:23
  • I just wanted to add that there are more problems with uninitialized variables earlier. – Gerhardh Nov 23 '18 at 08:55
0

You have a bit of a mess in your variale declarations:

int main (void)
{
    int x;
    int T,F[x],G[x];    <==== x is not initialized. You define arrays of random length. Might be 0.
    x= T+1;             <==== T is not initialized.
    printf("Enter the value of T: ");
    scanf("%d",&T);     <==== now T has a value but it is never used.
    convolution(F,G);
    return 0;
}

Then in the called function the same problems are repeated:

void convolution (int F[ ] ,int G [ ])
{
    FILE *fptr;
    int i,j,T;
    int x;
    F[T],G[T];   <=== T is not initialized; You access random elements of arrays of unknown random length.
    x= T+1;      <=== T is still not initialized.

Then desaster accelarates... ;)

for (i=0; i<=x; i++)  <== x is T+1 (still uninitialized) 
{
    F[i]= i+3;   <== With i going up to x which is T+1 you access index T+1 of an array of size [T] which is out of bounds.
    fscanf(fptr,"%d",&j);
    printf("F[%d] = %d\n",i,F[i]);
}

You should start all over with your variables.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39