Thread: Question for HWK.

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    39

    Question for HWK.

    Hi, we're learning Functions right now, and in lecture, he goes over the basics of it. How to set the function up, etc, etc.. Then he assigns complicated homework based on the easy-oriented lectures and expects us to know how to do it.

    Could someone give me some guidance on how to start with this homework?

    Question for HWK.-capture-png

  2. #2
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    We also haven't really went over arrays, other than they're able to store multiple different variables of the same type. I'm not sure how to define an array like that or associate each individual element with one another.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would just give it a try. Even though they are called matrices in the homework, they are 1 row so they may as well be arrays. Every A in the description is part of one array A, every B in the description is part of another array B. And you add together each element to make the elements of array C. He's not exactly being unfair here.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well, there wouldn't be an array C... excuse me. Dot products are a single number.

  5. #5
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Quote Originally Posted by whiteflags View Post
    Well, there wouldn't be an array C... excuse me. Dot products are a single number.

    Thanks for the feedback, yeah he kind of just sprung this on us Friday and it's due Tuesday.

    How do you get user input for elements of an array?

    example as in homework, I have two matrices, how do I get the user to input the elements for myArray1 and myArray2?

    I guess what I'm trying to ask is how do I input the scanned in numbers into the array / matrix.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Right, well, it would look something like what you learned for entering in numbers in general.

    Code:
    int numbers[10];
    int i;
    for (i = 0; i < 10; ++i) {
       printf("Enter number %d:", i);
       scanf("%d", &numbers[ i ]);
    }
    I don't know how much you know about arrays, but you just choose the array element i that you want to change and then pass the address (&) to scanf() like normal. In the example, you'll notice that all of the numbers will get values. Notice that the first one is in position 0, and that the last one is 9... this is true for all arrays of size N (elements are numbered from 0 to N-1).

  7. #7
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Okay, I took your advice and decided just to give it a try. You're right, I don't have much experience with arrays, haha.

    Code:
    int dotproduct();
    
    
    
    
    
    
    
    
    
    
    int dotproduct()
    {
    
    
    int array1[5];
    int array2[5];
    int dot[5];
    int sum;
    
    
    for(int i = 0; i <= 5; i++) // Gets elements for first matrix
    {
       printf("[Matrix 1] Enter number %d: \n", i);
       scanf("%d", &array1[i]);
    }
       
    for(int i = 0; i <= 5; i++) // Gets elements for second matrix
    {
       printf("[Matrix 2] Enter number %d: \n", i);
       scanf("%d", &array2[i]);
    }
    
    
    for(int i= 0; i<=5; i++) // Calculates A1*B1 , A2*B2, A3*B3, etc...
    {
        dot[i] = array1[i] * array2[i]
    }
    
    
    
    
        sum = dot[0] + dot[1] + dot[2] + dot[3] + dot[4] + dot[5]
        
    
    
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That seems about right, though. Good work!

    The only thing I would change for certain is that I would make all of the for loops strictly less than.
    Code:
    for (int i = 0; i < 5; i++)
    There is no element 5, so don't count it.

  9. #9
    Registered User
    Join Date
    Oct 2017
    Posts
    39
    Now they taught us we can return a value from the function, to do so, how do i get sum back to the main function? I was thinking to add
    Code:
     return sum;
    to the end of the function code, then,

    in the main,

    Code:
    printf("Your vector dot addition sum is %d", dotproduct());

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    That would work fine.

  11. #11
    Banned
    Join Date
    Aug 2017
    Posts
    861
    I think you missed a step. It looks like you're doing everything in the function, whereas the ass. says b: function to pass in two arrays in the prams, then do the 'math' on them then return back the results.

    Code:
    int dotproduct(int arr1[ ] , int arr2[ ]) 
    {
    
     do math code whatever you got a do
    
    return total;
    }
    
    int main(void)
    {
    int arr1[10], arr2[10];
     get input
    
    printf("Your vector dot addition sum is %d", dotproduct(arr1,arr2));
    
    return 0;
    }
    Last edited by userxbw; 10-29-2017 at 03:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-25-2014, 05:41 PM
  2. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  3. *szString = things question/memory question
    By Jang in forum C Programming
    Replies: 3
    Last Post: 01-20-2011, 04:59 AM
  4. Newbish Question file reading question....
    By kas2002 in forum C Programming
    Replies: 23
    Last Post: 05-17-2007, 12:06 PM
  5. Self regiserting DLLs question and libraries question.
    By ApocalypticTime in forum Windows Programming
    Replies: 2
    Last Post: 03-22-2003, 02:02 PM

Tags for this Thread