Thread: Need help with for loops and arrays

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    9

    Need help with for loops and arrays

    Hello, I'm currently a student with a beginner programming skill level and have trouble with programming problem.

    The problem is:

    Let X and Y be two arrays of size 10. Let Z be an array of size 19. The values of arrays X and Y are available in the
    file "input-xy.txt", where every line contains three values: (1) index of the array, i -- an integer; (2) value of element
    in array x, x[i] -- a double value, and (3) value of element i in array y, y[i] -- a double value. The value of x and y ele-
    ments for undefined indices are assumed to be zero.

    Element j of array z, z[j] is computed as below: j = 0, 1, 2, . . . , 18.

    for the summation:
    http://i36.tinypic.com/1roldu.jpg

    Now what I currently have is

    Code:
    #include <stdio.h>
    
    FILE *inp;
    
    main()
    {
    	int i, k, j, x[10], y[10], z[19];
    	int sum = 0
    	
    	inp = fopen("input-xy.txt", "r");
    
    	fscanf(inp, "%d ", &b);
    	fscanf(inp, " %d %d ", &x[b], &y[b]);
    	printf(" %d %d %d ", b, &x[b], &y[b]);
    	
    	for ( j = 0; j <= 9; j++)
    	{
    			k = 0;
    			z[j] = x[k] * y[j-k];
    	
    	for ( j = 11; j <=19; i++)
    	{
    What I want to know does everything look alright? I'm making my k max equal 0 for 0<j<9 so everything would be simplified. That would be all my values for x are less than or equal to 9.

    But also can I do this:
    z[j] = x[k] * y[j-k] in the for loop? Can I write a function with the array bracket?
    Last edited by Skeeter; 10-23-2009 at 12:37 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    b is not a declared variable, so no. It can't fly like that.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    9
    Quote Originally Posted by Adak View Post
    b is not a declared variable, so no. It can't fly like that.
    Opps I mean't for that too to be j.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Assuming you manage to fix the rest of the b's, and add a semicolon where necessary, then the reading in is good. For the summation, you will have to sum things together.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, once you get the j in there. Then there's this bit:

    Code:
    	
    	for ( j = 0; j <= 9; j++)
    	{
    			k = 0;
    			z[j] = x[k] * y[j-k];
    	
    	for ( j = 11; j <=19; i++)
    	{
    Which is indented to look like the second for loop is outside the first, and it's not, clearly. You either need another counter variable for the second for loop, or you need to close the first for loop, before starting the second one.

    I'm not sure what you're asking here:
    z[j] = x[k] * y[j-k] in the for loop? Can I write a function with the array bracket?
    That code is already inside your first for loop, and "writing a function with the array bracket" leaves me adrift.
    Last edited by Adak; 10-23-2009 at 12:45 PM.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    9
    Quote Originally Posted by Adak View Post
    OK, once you get the j in there. Then there's this bit:

    Code:
    	
    	for ( j = 0; j <= 9; j++)
    	{
    			k = 0;
    			z[j] = x[k] * y[j-k];
    	
    	for ( j = 11; j <=19; i++)
    	{
    Which is indented to look like the second for loop is outside the first, and it's not, clearly. You either need another counter variable for the second for loop, or you need to close the first for loop, before starting the second one.

    I'm not sure what you're asking here:


    That code is already inside your first for loop, and "writing a function with the array bracket" leaves me adrift.
    Yeah it seems I forgot to close the first loop, what I'm asking for the second part is could I put something like y[j-k], the j-k function within the bracket for it to identify itself with a certain index.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Absolutely yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Help with arrays and pointers please...
    By crazyeyesz28 in forum C++ Programming
    Replies: 8
    Last Post: 03-17-2005, 01:48 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM