help with simple programs. Matrix multiplications

This is a discussion on help with simple programs. Matrix multiplications within the C Programming forums, part of the General Programming Boards category; hello, I am having trouble with a few simple programs... Can someone see if they can fix my problems and ...

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    1

    help with simple programs. Matrix multiplications

    hello,
    I am having trouble with a few simple programs... Can someone see if they can fix my problems and get back to me?

    I attached a doc file.. Please highlight the mistakes with notes if possible so I can learn from them..

    EDITED... I will post the text below:

    -----------------------------------------------------------------------
    FIRST PROGRAM

    This program is supposed to call on the function called multiply to multiply two matrices, one is 3x3 and the other is 3x2. The two matrices are called A and B, and the product matrix is called C. They are supposed to be float arrays.
    This program compiles, but I am having problems when I input the numbers. It gives me weird values in return or drops the program. The input is supposed to be of the form:
    1,0,1
    -1, 2, 1
    3, 0, 0
    0, 1
    1, 1
    0, 0
    for example.
    Don’t worry about how the resulting array C is supposed to be printed, it’s a weird format.


    [tag]
    Code:
    #include <stdio.h>
    
    void multiply(float A[3][3], float B[3][2]){
            float C[3][2];
            int i, s;
    *the nested for loops go through the elements of the arrays A and B to multiply the numbers, and places the output in array C*
            for (s=0; s<=2; s++){
                    for (i=0; i<=2; i++){
                            C[s][0] = A[s][i] * B[i][0] + C[s][0];
                            C[s][1] = A[s][i] * B[i][1] + C[s][1];
                    }
            }
    }
    
    main () {
            int j, m, t;
            float A[3][3];
            float B[3][2];
            float C[3][2];
    
            printf("enter the arrays you wish to multiply:\n"); *prompts user input*
    
    *these for loops are for scanning in each inputted number and placing it in the appropriate place in the array*
            for (j=0; j<=2; j++){
                            scanf ("%f%f%f", &A[j][0], &A[j][1], &A[j][2]);
            }
    
            for(m=0; m<=2; m++) {
                            scanf ("%f%f", &B[m][0], &B[m][1]);
            }
    *calls on the function “multiply”*
            multiply(A, B);
    *this prints out the resulting array, C, in a matrix-like format, don’t worry about this part*
            printf(" _   _ \n");
            for(t=0; t<=2; t++){
                    printf("|%f , %f|\n", C[t][0], C[t][1]);
            }
            printf(" -   - \n");
    }
    [/tag]
    --------------------------------------------------------------------
    NEXT PROGRAM


    This program is supposed to call on the function find with parameters str and substr. Basically a user inputs a string (str) and then another string (substr), and the program is supposed to see if substr is located in str. If it is not, the function is supposed to return –1. This program will not compile. There are 2 errors listed and are pointed out in the program.
    The user input is supposed to look like:
    I_like_cheese
    For example, with “_” instead of spaces. And then if someone inserts “cheese” for substr, it is supposed to tell you that it is in str and is at position 7.

    [tag]
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void find(char str[], char substr[]) {
            int i;
            int j = sizeof(substr)/sizeof(int); *These get the sizes of each array for length*
            int k = sizeof(str)/sizeof(int);
    
            for (i=0; i<=(k-j); i++) {
                    if (str[i][j+i] = substr) *there is an error here saying “subscripted value is neither array nor pointer”. I am trying to shift over the set of values in the array every time to see if it matches substr. I don’t think str[i][j+i] is correct notation.*
                          return i;
                    else
                          return -1; *there is also an error saying  “warning: `return' with a value, in function returning void” for both return i; and return –1.*
            }
    }
    
    int main () {
            int i;
            char str[100];
            char substr[100];
    
            printf ("enter the string you wish to search:\n"); *prompts user input*
            scanf ("%s", &str);
            printf ("enter the term you wish to find in the string %s:\n", str);
            scanf ("%s", &substr);
    
            find(str, substr); *calls on function “find”*
    
            if (-1)  *depending on what is returned from “find” it prints the results*
                    printf ("%s is not in the string %s\n", substr, str);
            else
                    printf ("%s is in the string %s at position %d", substr, str, i);
    }
    [/tag]

    Regards,
    ovadoggvo
    Attached Files Attached Files
    Last edited by ovadoggvo; 05-18-2005 at 10:53 PM.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    It's HORRIBLE to see some C source into a word file!!!
    It should be illegal !!!!

    I don't have time to check ur code, sorry!!! I'm tired! but I'm sure that my code for matrix multiplication will help you

    Code:
    /* Ex2 BianConiglio */
    // I had no time to implement the command line version
    // of the program, anyway I should use argv[i] to set
    // rows and columns, the rest will be the same.
    
    #include "stdio.h"
    #include "stdlib.h"
    #include "string.h"
    
    // matrix definitions
    #define RIGAA 2 // row of A
    #define COLA  3 // col of A
    #define RIGAB 3 // row of B
    #define COLB  4 // col of B
    #define MAX_MATRIX_VALUE 5 // max number in a matrix cell
    
    // global variables, matrixes
    int A [RIGAA][COLA];
    int B [RIGAB][COLB];
    int C [RIGAA][COLB];
    
    void GenerateMatrixes ();
    void GenerateMatrix (int* matrix, int row, int col);
    void MultiplyaMatrix ();
    void PrintMatrix (char* name, int* matrix, int row, int col);
    void CleanScreen ();
    
    
    int main (int argc, char* argv[]) {
      int ch = 0;  //user choice
      
      CleanScreen ();
      
      printf ("************************************\n");
      printf ("**     Matrix Multiplication      **\n");
      printf ("************************************\n\n");
    
      GenerateMatrixes ();
      
      while (ch != 5) { // loop until exit
        ch = 0; // reinitialization
        printf ("Regenerate matrixes  : 1 \n");
        printf ("Multiplicate         : 2 \n");
        printf ("Print matrixes       : 3 \n");
        printf ("Clean Screen         : 4 \n");
        printf ("Exit                 : 5 \n");
        printf ("Choose command       : ");
        scanf  ("%d", &ch);
        
        switch (ch) {
        case 1: GenerateMatrixes ();
          break;
        
        case 2: MultiplyaMatrix ();
          break;
          
        case 3: PrintMatrix ("A", (int*)A, RIGAA, COLA);
          PrintMatrix ("B", (int*)B, RIGAB, COLB);
          break;
          
        case 4: CleanScreen ();
          break;
    
        default: CleanScreen (); // default behavior
          break;
        };
      }
      return 0;
    }
    
    
    // random generation and population of matrix values
    void GenerateMatrixes () {
       
      // generate A B
      GenerateMatrix ((int*)A, RIGAA, COLA);
      GenerateMatrix ((int*)B, RIGAB, COLB);
      
      // print matrixes
      PrintMatrix ("A", (int*)A, RIGAA, COLA);
      PrintMatrix ("B", (int*)B, RIGAB, COLB);
    }
    
    //Print a matrix
    void PrintMatrix (char* name, int* matrix, int row, int col) {
      int i, j; // indexes
      
      printf ("\nMatrix %s: \n\n", name);
      
      for (i = 0; i < row; i++) { // rows
        for (j = 0; j < col; j++) { // col
          printf ("%d  ",matrix[(i*col)+j]); // print item
        }
        printf ("\n");
      }
      printf ("\n");
    }
    
    //Matrix multiplication
    void MultiplyaMatrix () {
      int i, j, m; // indexes
      
      for (i = 0; i < RIGAA; i++)
        for (j = 0; j < COLB; j++) {
          C[i][j] = 0;  // initialization of the new matrix
          for (m = 0; m < COLA; m++)
    	C[i][j] += (A[i][m] * B[m][j]);
        }
      PrintMatrix ("C", (int*)C, RIGAA, COLB);
    }
    
    // Generate random matrix
    void GenerateMatrix (int* matrix, int row, int col) {
      int i, j = 0; //indexes
      
      for (i = 0; i < row; i++) // row
        for (j = 0; j < col; j++) //col
          matrix[(i*col)+j] = rand() % MAX_MATRIX_VALUE; // populate
    }
    
    
    void CleanScreen () {
      system ("clear"); // unix system
    }
    check it out!
    This forum is the best one I've ever seen. Great ppl, great coders

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 04:57 PM
  2. Need a quick example! Operator overloading and matrix
    By Great Satchmo in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2004, 10:08 PM
  3. Simple Matrix Operations?
    By devin in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2003, 11:10 PM
  4. Help me with these simple programs
    By Help me in forum C Programming
    Replies: 4
    Last Post: 11-08-2001, 09:38 AM
  5. Need help with simple programs...
    By BCole19 in forum C++ Programming
    Replies: 22
    Last Post: 08-30-2001, 09:45 PM

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