Problem with fscanf statement

This is a discussion on Problem with fscanf statement within the C Programming forums, part of the General Programming Boards category; Yes that's true. I forgot about the semicolon. I had it right when using the terminal to compile it so ...

  1. #16
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    Yes that's true. I forgot about the semicolon. I had it right when using the terminal to compile it so it did compile. But the code you just wrote still says segmentation error.

  2. #17
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    nevermind. Thanks.

  3. #18
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    ok so now it executes but it gives a lot of numbers all randomly which i don't really understand why. I'm trying to write code to do the summation of x[k] * y[j-k] from k=max(j-9,0) to min(9,j) and calling this array z[j]. I have to write a program that reads the input for x and y arrays from the input file and compute the z array. The program is suppose to print every element of array z. And j=0,1,2,....,18.
    The input file contains three values: (1) index of the array, i; (2) value of element i in array x, x[i]; and (3) value of element i in array y, y[i];
    the value of x and y elements for undefined indices are assumed to be zero.
    I think I am really close. Maybe you can help me fill in the missing code that i need.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why do you want to compute z every time? Do you need to compute it every time? Why wouldn't you just compute once after you read all the numbers in?

    the value of x and y elements for undefined indices are assumed to be zero.
    Perhaps by you, but not by anyone (or anything) else. If you need them to be zero, set them to zero.

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    Yeah i have to compute the z array for z[1], z[2], z[3], and so on until z[18].

    And I can set them to zero after I get the program to do what I want it to do.

  6. #21
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes, but you can't compute until you read all the data in. And z and x and y must all be the same size; 10 or 19 doesn't matter to me, but you'll have to pick one.

  7. #22
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    why do they have to be the same size? I'm just giving you what the assignment states. It states that x is size 10, y is size 10, and z is size 19.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because computing z[15] (say) requires y[15] to exist (when j = 0 in your formula, you will then try to multiply it by y[15] -- and therefore y[15] must exist, which means that y has to be declared as at least that size).

  9. #24
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    No cause its x[k] and y[j-k] so when j is 15, then k is j-9 which gives 6 so its x[6]*y[9] = z[15]

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, yes, so your code posted does. And if you run that code you would get
    Code:
    array z[1] is 2.000000
    array z[2] is 3.000000
    array z[3] is 4.000000
    array z[4] is 122176384420437770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
    array z[5] is 2278088.760784
    array z[6] is 121297690267566810000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
    array z[7] is 122172951969028870000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
    array z[8] is 23162634409391300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
    array z[9] is 10.000000
    array z[10] is 27601030745683142000000000000000000000000.000000
    array z[11] is 8.000000
    array z[12] is 5302421546766900900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
    array z[13] is 6.000000
    array z[14] is 5.000000
    array z[15] is 4.000000
    array z[16] is 3.000000
    array z[17] is 2.000000
    array z[18] is 122176384420437770000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.000000
    which isn't a segmentation fault, although it does show that you failed to initialize your array of z's to zero before you started.

  11. #26
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    I don't get that. I get it doing the same thing over and over again in a loop having z[0] = 1 and then z[0] = 2 and so forth until z[0] = 10. Which i really don't get.

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's because I took it out of the loop, because I would guess it doesn't belong there.

  13. #28
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    what did you take out of the loop?

  14. #29
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The calculation of z. You wouldn't want to do a convolution with incomplete data, I wouldn't think.

  15. #30
    Registered User
    Join Date
    Nov 2008
    Posts
    27
    I'm confused. What data is incomplete? And taking it out of the loop doesn't do anything. Do i have to take it out of both of the loops? Or out of the while loop completely?

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with a switch statement.
    By Merholtz in forum C Programming
    Replies: 3
    Last Post: 10-19-2008, 04:21 PM
  2. Multithreading problem
    By Bacardi34 in forum C Programming
    Replies: 5
    Last Post: 09-02-2004, 02:26 PM
  3. Replies: 1
    Last Post: 08-31-2004, 04:07 AM
  4. problem using strtok and fscanf together
    By osal in forum C Programming
    Replies: 4
    Last Post: 07-20-2004, 02:21 PM
  5. If statement re-do problem
    By RoD in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 04:46 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21