Matrices: Different dimensions + implementation - c

I'm trying to implement 2 matrices that I must use without having user input and multiplying it with each other. However, they have different dimensions. How would I be able to code the this? I literally tried everything that i deleted my code and restarting it.
main(void){
int B[4][3] = {{1,0,0}, {1,0,1},{1,1,0},{1,1,1}};
int Y[1][4] = {{1}, {1}, {1}, {0}};
}

int Y[1][4] = {{1}, {1}, {1}, {0}};
is not right. That would be good for a 4 x 1 array.
int Y[4][1] = {{1}, {1}, {1}, {0}};
What you need is:
int Y[1][4] = {{1, 1, 1, 0}};
Now you can multiply.
int main ()
{
int B[4][3] = {{1,0,0}, {1,0,1},{1,1,0},{1,1,1}};
int Y[1][4] = {{1, 1, 1, 0}};
int R[1][3] = {0};
for ( int i = 0; i < 1; ++i )
{
for ( int j = 0; i < 3; ++j )
{
for ( int k = 0; i < 4; ++k )
{
R[i][j] += Y[i][k]*B[k][j]
}
}
}
}

Related

How do I solve this array problem in C language?

I have 2 arrays:
int element[3] = {0, 1, 2};
int quantity[3] = {2, 3, 4};
Now I want a result array that will have two zeros, three ones and four twos.
int result[2+3+4] = {0, 0, 1, 1, 1, 2, 2, 2, 2};
How do I do this using loop?
You need to count the number of elements in the result array and to declare either a variable length array with the calculated value or to allocate dynamically such an array.
For example
int quantity[3] = {2, 3, 4};
size_t n = 0;
for ( size_t i = 0; i < 3; i++ )
{
n += quantity[i];
}
int result[n];
// or
// int *result = malloc( n * sizeof( int ) );
And then in nested loops you need fill the resulted array.
For example
for ( size_t i = 0, j = 0; i < 3; i++ )
{
for ( size_t k = 0; k < quantity[i]; k++ )
{
result[j++] = element[i];
}
}
Firstly we need to calculate the size of the result array. Then start populating your result array each element at a time. While we populate the result array, we need to increment the index.
int elementSize = sizeof(element)/sizeof(element[0]);
int resultSize = 0;
//pre calculating the size of result array
for(int i=0;i<elementSize;i++ ) {
resultSize += quantity[i];
}
int result[resultSize], currIndex = 0;
//picking each element
for(int i = 0;i< elementSize; i++ ) {
int currElement = element[i];
int currQuantity = quantity[i];
//filling the current element required no of times in the result array
while(currQuantity--) {
result[currIndex] = currElement;
currIndex++;
}
}
//just a for loop to check the elements inside result array
for(int i=0;i<resultSize;i++)
printf("%d\n",result[i]);

Multiply matrices in C using 3 arrays

I am trying to write an algorithm that calculates 2 nxn matrices using 3 arrays per matrix.
For example
int m1Row[] = {1,1,2,2};
int m1Column[] = {1,2,1,2};
int m1Value[] = {1,2,3,4};
int m2Row[] = {1,1,2,2};
int m2Column[] = {1,2,1,2};
int m2Value[] = {4,3,2,1};
represents 2 matrices. m1Value[x] is the value of matrix 1 at row m1Row[x] and column m1Column[x].
m1:
1 2
3 4
m2:
4 3
2 1
The code I wrote somehow works for some matrices but not for every one. The problem is probably that I am missing the part where you have to add some values to each other to multiply two matrices. Here's an example from wikipedia.
Example of correctly calculated matrices (m = count of numbers not equal to zero):
int m = 4;
int n = 8;
int m1Row[] = {1, 2, 3, 3};
int m1Column[] = {1, 2, 1, 3};
int m1Value[] = {1, 2, 3, 4};
int m2Row[] = {1, 2, 3, 3};
int m2Column[] = {3, 2, 1, 2};
int m2Value[] = {7, 3, 1, 2};
I already thought about this problem for hours and tried some other for-loops and everything but it just made it worse and no value was correct or it calculated way too many values.
I hope somebody can help me get this code working so that the result matrix is calculated correctly.
My code:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
//************************
int m = 4;
int n = 8;
int m1Row[] = {1, 2, 3, 3};
int m1Column[] = {1, 2, 1, 3};
int m1Value[] = {1, 2, 3, 4};
int m2Row[] = {1, 2, 3, 3};
int m2Column[] = {3, 2, 1, 2};
int m2Value[] = {7, 3, 1, 2};
//************************
int resultRow[50]; //result matrix, 50 is just a placeholder
int resultColumn[50];
int resultValue[50];
int reminder = 0;
int resultBuffer = 0;
int saveIndex = 0;
for(int i = 1; i <= n;) {
if(m1Row[reminder] == i) {
for(int j = 0; j < m; j++) {
if(m2Row[j] == m1Column[reminder]) {
resultBuffer = m2Value[j] * m1Value[reminder];
//save result
resultRow[saveIndex] = m1Row[reminder]; //save row number
resultColumn[saveIndex] = m2Column[j]; //save column number
resultValue[saveIndex] = resultBuffer; //save value
saveIndex++;
}
}
reminder++;
} else { i++; }
}
//print results
printf("resultRow[] resultColumn[] resultValue[]\n");
for(int i = 0; i <= reminder; i++) {
printf("\t%d\t\t\t\t%d\t\t\t%d\n", resultRow[i], resultColumn[i], resultValue[i]);
}
return 0;
}

Formatting arrays in corelation to ther arrays C

I am trying to make a function that is able to recognize if Winning_order arrays integers are within Order1,Order2,Order3. In Order1 the first row of Winning_order is present {1,2,3} as well as in order2. However in Order3 none of the elements correlate to the values of Winning_order so it is not a valid output.
int main(void)
{
int Order1[5] = {1,2,3}
int Order2[5] = {1,2,5,3}
int Order3[5] = {1,2,5}
int Winning_order[5][3] = {{1,2,3}, {4,5,6}, {7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};
return 0;
}
Expected Output:
Order1
Order2
To expand on Jack Lilhammers's comment, you could do something like this:
#include <stdio.h>
int match_arrays(int *arr1, int *arr2, int len) {
for (int i = 0; i < len; i++) {
if (arr1[i] != arr2[i]) {
return 0;
}
}
return 1;
}
int main(void)
{
int Order1[3] = {1,2,3};
int Order2[4] = {1,2,5,3};
int Order3[3] = {1,2,5};
int Winning_order[8][3] = {{1,2,3}, {4,5,6}, {7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}};
for (int i = 0; i < 5; i++) {
if (match_arrays(Order1, Winning_order[i], 3)) {
printf("Order1");
}
if (match_arrays(Order2, Winning_order[i], 3)) {
printf("Order2");
}
if (match_arrays(Order3, Winning_order[i], 3)) {
printf("Order3");
}
}
return 0;
}
Be cause those are statically declared arrays, you have to use a series of if statements, or create a new array out of them, and it forces a lot to be hard coded.
EDIT: fixed include, dimensions, removed breaks, fixed function calls
Firstly, as #Jack Lilhammers pointed out, your expected output is incorrect if we can assume we understand what you are trying to accomplish: Your problem is essentially to check if an array is an element of an array-of-arrays.
This can be achieved like this:
#include <stdio.h>
int is_array_element(int rows, int columns, int arr1[rows][columns],
int arr2[columns]) {
for(int i = 0; i < rows; i++) {
for(int j = 0; j < columns; j++) {
if(arr1[i][j] != arr2[j]) {
break;
}
if(j == columns - 1) {
return 1;
}
}
}
return 0;
}
int main(void) {
/* as Order1 and Order3 have been declared as 5-arrays but are only
initialized with 3 integers, the remaining two elements are zero and
can (should) be ignored */
int Order1[5] = {1, 2, 3};
/* not clear how you wish to compare a 4-array with a 3-array and so
the code will clip the last element */
int Order2[5] = {1, 2, 5, 3};
int Order3[5] = {1, 2, 5};
int Winning_order[8][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9},
{1, 4, 7}, {2, 5, 8}, {3, 6, 9},
{1, 5, 9}, {3, 5, 7}
};
if(is_array_element(8, 3, Winning_order, Order1)) {
printf("Order1\n");
}
if(is_array_element(8, 3, Winning_order, Order2)) {
printf("Order2\n");
}
if(is_array_element(8, 3, Winning_order, Order3)) {
printf("Order3\n");
}
return 0;
}
And the output we get is:
Order1

How to use block in c

I have one block with integers, I need to do some task in school.
I need to group even numbers to one block (even numbers)
and i need to group odd numbers to one block (odd numbers)
I was trying to do something, but this doesn't work normally
#include <stdio.h>
#include <stdlib.h>
int main()
{
int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
int parostomb[] = {};
int paratlantomb[] = {};
int n = 9;
int i = 0;
int j = 0;
int paros = 0;
int paratlan = 0;
for (i=0; i < n; i++){
if (tomb[i]%2 == 0) {
parostomb[paros] = tomb[i];
paros = paros + 1;
printf("paros: %d\n", parostomb[paros]);
}
else {
paratlantomb[paratlan] = tomb[i];
paratlan = paratlan + 1;
printf("Paratlan:%d\n", paratlantomb[paratlan]);
}
}
return 0;
}
Please help
If you run the code you will be see the code is doesn't work normally
Here
int parostomb[] = {};
int paratlantomb[] = {};
you do not set the array size.
You need to change it to:
int parostomb[sizeof(tomb)/sizeof(int)] = { 0 };
int paratlantomb[sizeof(tomb)/sizeof(int)] = {0 };
So what is sizeof (tomb)/sizeof(int) doing? It calculates the number of elements in the array tomb. The first part sizeof(tomb) calculates the number of bytes in the array. The second part sizeof(int) calculates the number of bytes in an int. Consequently, diving the first by the second will give you the number of int in the array (aka the number of array elements).
sizeof(tomb)/sizeof(int) can also be written sizeof tomb/sizeof *tomb. The later is typically considered the best because in case you to change the array type, e.g. use long int instead of int, you don't have to change sizeof(int) to sizeof(long int). When using sizeof *tomb the actual type is handled automatically.
Further here:
parostomb[paros] = tomb[i];
paros = paros + 1;
printf("paros: %d\n", parostomb[paros]);
You increment paros before printing so you do not print the newly assigned value.
Change to:
parostomb[paros] = tomb[i];
printf("paros: %d\n", parostomb[paros]);
paros = paros + 1;
Further here:
int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
...
int n = 9; <-------------------- The array size is only 8
To avoid such bugs do:
int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
...
int n = sizeof(tomb)/sizeof(int);
Putting it all together, your code should be:
int main(void)
{
int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
int parostomb[sizeof(tomb)/sizeof(int)] = {0};
int paratlantomb[sizeof(tomb)/sizeof(int)] = {0};
int n = sizeof(tomb)/sizeof(int);
int i = 0;
int paros = 0;
int paratlan = 0;
for (i=0; i < n; i++){
if (tomb[i]%2 == 0) {
parostomb[paros] = tomb[i];
printf("paros: %d\n", parostomb[paros]);
paros = paros + 1;
}
else {
paratlantomb[paratlan] = tomb[i];
printf("Paratlan:%d\n", paratlantomb[paratlan]);
paratlan = paratlan + 1;
}
}
}
Output
Paratlan:5
paros: 10
paros: 8
Paratlan:3
Paratlan:-1
paros: 4
paros: 2
Paratlan:9
That said... It seems strange that you save the numbers into two arrays that you never really use. It would make more sense to generate the arrays first and then print them in separate loops. Like:
int main(void)
{
int tomb [] = {5, 10, 8, 3, -1, 4, 2, 9};
int parostomb[sizeof(tomb)/sizeof(int)] = {0};
int paratlantomb[sizeof(tomb)/sizeof(int)] = {0};
int n = sizeof(tomb)/sizeof(int);
int i = 0;
int paros = 0;
int paratlan = 0;
for (i=0; i < n; i++){
if (tomb[i]%2 == 0) {
parostomb[paros] = tomb[i];
paros = paros + 1;
}
else {
paratlantomb[paratlan] = tomb[i];
paratlan = paratlan + 1;
}
}
printf("Even numbers: ");
for (i=0; i < paros; i++){
printf("%d ", parostomb[i]);
}
printf("\n");
printf("Odd numbers: ");
for (i=0; i < paratlan; i++){
printf("%d ", paratlantomb[i]);
}
printf("\n");
}
Output
Even numbers: 10 8 4 2
Odd numbers: 5 3 -1 9

How to return largest two numbers in array in C?

So, I have this so far. I'm trying to find the two largest numbers in an array and return them. I looked up a lot of resources online, and most of them say "call by reference" is the way to go. But I've no idea how to make it work with my program. For example, I saw this example online:
void Calculate(int x, int y, int* prod, int* quot)
{
*prod = x*y;
*quot = x/y;
}
int x = 10,y = 2, prod, quot;
Calculate(x, y, &prod, &quot)
How does the above program actually "return"? How do I print the return values to the console?
#include "stdio.h"
void largest_two( int numbers[], int len, int *largest, int *next_largest){
int i, temp;
*largest = numbers[0];
*next_largest = numbers[1];
if(*largest < *next_largest){
temp = *next_largest;
*largest = *next_largest;
*next_largest = temp;
}
for (i=0; i<sizeof(numbers); i++) {
if(numbers[i]>= *largest){
*largest = numbers[i];
*next_largest = *largest;
}
else if ( numbers[i] > *next_largest){
*next_largest = numbers[i];
}
}
}
int main() {
int numbers[] = {3, 1, 2, 3, 6, 2, 8, 0, 0, 0};
int len = 3;
int largest, next_largest;
//==>??? printf("%d %d", largest_two(numbers, len, &largest, &next_largest));
}
Sides' from the pointer issues (you should read a tutorial / book on them), your main problem is that you're attempting to print the single return value of a function with return type void which means it won't return at all.
Your code:
int main() {
int numbers[] = {3, 1, 2, 3, 6, 2, 8, 0, 0, 0};
int len = 10; // sizeof(numbers)
int largest, next_largest;
largest_two(numbers, len, &largest, &next_largest);
printf("%d %d", largest, next_largest);
}
Keep in mind this is still not entirely correct, but it does adress your problem of printing the numbers.
Also, passing len means you shouldn't do this for (i=0; i<sizeof(numbers); i++) but this instead for (i=0; i<len; i++)
Firstly, this line:
for (i=0; i<sizeof(numbers); i++)
is not correct. You want this to be instead:
for (i=0; i<len; i++)
which should be passed to largest_two() as sizeof numbers/sizeof numbers[0], which is the actual length of the array.
I also suggest setting largest and next_largest to INT_MIN from <limits.h>, and then finding these values from their. It seems you are also having trouble with pointers, and it would be best to use them only when needed.
Here is an example which simplifies your approach, which finds the largest and second largest element in one loop of the array. It also only uses pointers when needed.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define ARRAYSIZE(x) (sizeof x/sizeof x[0])
void largest_two(int numbers[], size_t len, int *largest, int *next_largest);
int main(void) {
int numbers[] = {3, 1, 2, 3, 6, 2, 8, 0, 0, 0};
int largest, next_largest;
largest_two(numbers, ARRAYSIZE(numbers), &largest, &next_largest);
printf("largest = %d\nnext_largest = %d\n", largest, next_largest);
}
void largest_two(int numbers[], size_t len, int *largest, int *next_largest) {
int max, smax;
max = smax = INT_MIN;
for (size_t i = 0; i < len; i++) {
if (numbers[i] > max) {
smax = max;
max = numbers[i];
} else if (numbers[i] > smax && numbers[i] < max) {
smax = numbers[i];
}
}
*largest = max;
*next_largest = smax;
}
Output:
largest = 8
next_largest = 6
Second dataset:
int numbers[] = {3, 1, 6, 3, 6, 2, 8, 0, 8, 7};
Output:
largest = 8
next_largest = 7

Resources