# help with simple programs. Matrix multiplications

• 05-18-2005
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,
```/* 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 }```