Unable to specify starting and ending rows in matrix multiplication - c

I wrote a c program for matrix multiplication. Now I want to divide the number of rows into 5 blocks of N/5. Function should compute first block of rows in first iteration, second part in second iteration and so on. How do I specify starting row and ending row in each iteration ? I have tried it below. Could anyone help me correcting it.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, m, n, p, q, c, d, k,g, sum = 0,start=0,end=m/5;
int **first, **second, **multiply;
printf("Enter the number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Value entered %d%d \n",m,n);
first = malloc(m*sizeof(int*));
for ( i =0;i <m; i++)
first[i] =malloc(n*sizeof(int));
printf("Enter the number of rows and columns of second matrix \n");
scanf("%d%d", &p,&q);
printf("value entered %d%d \n",p,q);
second = malloc(p*sizeof(int*));
for( i=0;i<p;i++)
second[i] = malloc(q*sizeof(int));
multiply = malloc(m*sizeof(int*));
for ( i=0;i<m;i++)
multiply[i] = malloc(q*sizeof(int));
printf("Enter the elements of first matrix\n");
for( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
if ( n != p )
printf("Matrices with entered orders can't be multiplied with each other.\n");
else {
printf("Enter the elements of second matrix\n");
for ( c = 0 ; c < p ; c++ ){
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
}
for(g=0;g<5;g++){
for ( c = start ; c < end ; c++ ) {
for ( d = 0 ; d < q ; d++ ) {
for ( k = 0 ; k < p ; k++ ) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
start=start+m/5+1;
end=end+m/5;
}
printf("Product of entered matrices:-\n");
for ( c = 0 ; c < m ; c++ ) {
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);
printf("\n");
}
for ( i = 0; i < p; i++)
free(second[i]);
free(second);
for ( i = 0; i < m; i++)
free(multiply[i]);
free(multiply);
}
for ( i = 0; i < m; i++)
free(first[i]);
free(first);
return 0;
}

Start indices usually are 0 (read it always), but about ending indices,
You must save them in variables rows/cols, when you're allocating/constructing the matrix.

Related

Pyramid patterns using C

I need to do the following pattern using do-while, while or for.
I tried the following code but it prints the pattern only 1-5
I also tried to alter the n being 10 but then the spacing goes nuts.
#include <stdio.h>
int main(void)
{
int n = 5, i, j, num = 1, gap;
gap = n - 1;
for ( j = 1 ; j <= n ; j++ )
{
num = j;
for ( i = 1 ; i <= gap ; i++ )
printf(" ");
gap --;
for ( i = 1 ; i <= j ; i++ )
{
printf("%d", num);
num++;
}
num--;
num--;
for ( i = 1 ; i < j ; i++)
{
printf("%d", num);
num--;
}
printf("\n");
}
return 0;
}
Try replacing both occurences of:
printf("%d", num);
with
printf("%d", num % 10);
Now only the last digit will be shown.
After the change, for n set to 10 the program produces:
1
232
34543
4567654
567898765
67890109876
7890123210987
890123454321098
90123456765432109
0123456789876543210

How to find the maximum and sum of a row within a matrix

To the best of my knowledge what I have entered should be working properly, but I am new to C and am unable to come up with a reason as to why it isn't. Each function works fine separately but for some reason whilst together they produce weird errors.
#include <stdio.h>
#include<conio.h>
main()
{
int m, n, c, d, sum, matrix[10][10], maximum;
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of matrix\n");
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
scanf("%d",&matrix[c][d]);
}
}
maximum = matrix[0][0];
//Maximum
for( c = 0 ; c < m ; c++ )
{
for( d = 0 ; d < n ; d++ )
{
if ( matrix[c][d] > maximum )
maximum = matrix[c][d];
}
}
//Sum
for (c = 0; c < m; ++c)
{
for (d = 0; d < n; ++d)
{
sum = sum + matrix[c][d] ;
}
printf("Sum of the %d row is = %d\n", c, sum);
sum = 0;
}
printf("Maximum element in matrix is %d\n", maximum);
getch();
return 0;
}
You are operating addition operation on int sum before initializing it to 0. Initialize sum to 0 while declaring and that should fix your issue.

Is there a way to optimize the code and improve to K number of sum terms instead of 4?

#include <stdio.h>
int main() {
int N = 133;
int a, b, c, d;
int flag = 0;
for ( int j = 1; j < (N/2); j++)
{
a = j;
for ( int k = 1; k < (N/2); k++)
{
b = k;
for ( int l = 1; l < (N/2); l++)
{
c = l;
for ( int m = 1; m < (N/2); m++)
{
d = m;
if ( a+b+c+d == N && (a != 0 && b!= 0 && c != 0 && d!= 0))
{
printf("\n %d + %d + %d + %d = %d" , a, b, c, d, N);
flag = 1;
break;
}
}
if(flag)
break;
}
if(flag)
break;
}
if(flag)
break;
}
return 0;
}
The code currently outputs
1 + 2 + 65 + 65 = 133
As you can see, I am getting the sum using 4 numbers to form N (133) in this case. Is there a way to improve the code to 'k' numbers without using nested 'k' for loops?
Desired Output: ( a + b + c + d + e + f + ...... + k = N )
say for a given value of N represented using sum of k terms, where k is an input parameter.
Notes:
None of the 'k' terms is zero.
Original question had loops starting from 0; updated to start from 1.
Specific Requirement, I want the terms (a to k) to have the lowest possible standard deviation among all the sums. So breaking out at the first sum is not ideal for that scenario, but this is the baseline code I have reached. Once I figure out reducing number of loops, I know how to modify for lowest S.D.
Also pretty obvious but k < N in all cases.
Much simpler code that does the same thing as yours:
#include <stdio.h>
int main()
{
int N = 133;
for ( int j = 1; j < (N/2); j++)
for ( int k = 1; k < (N/2); k++)
for ( int l = 1; l < (N/2); l++)
for ( int m = 1; m < (N/2); m++)
if ( j+k+l+m == N) {
printf("\n %d + %d + %d + %d = %d" , j, k, l , m, N);
return 0;
}
}
And to your problem, which is a bit vague, but seems to be finding k numbers a_1, a_2 ... a_k such that 1 < a_n < N/2 for all n and a_1+a_2+...+a_k=N. Here is very simple code to do that, using your algorithm but extended for arbitrary k:
#define N 133
#define k 8
int main()
{
int arr[k];
for(int i=0; i<k; i++)
arr[i]=1;
for(int i=0,c=k; c<N; c++) {
arr[i]++;
if(arr[i]>=N/2) {
c--;
i++;
}
}
for(int i=0; i<k-2; i++)
printf("%d + ", arr[i]);
printf("%d = %d\n", arr[k-1], N);
}
It has no error checking. The problem is not solvable for k=1 and k>N. And because integer division is rounded down, it is not solvable for k=2 if N is odd.
But here is some MUCH more efficient code. The problem is very simple, so it's not about finding the numbers a_1, a_2, a_3 ... a_k. It's really only about finding a_1 and a_2. The rest are one.
#define N 19
#define k 5
int main()
{
for(int i=0; i<k-2; i++)
printf("1 + ");
int c=N/2-(k-2);
if (c<1)
c=1;
printf("%d + ", c);
printf("%d = %d\n", N-(k-2)-c, N);
}
Again, no error check is made.

display 25 numbers arranged 5 by 5 order

I am having a problem arranging 25 numbers into 5 by 5 order. This is what I have so far:
int main() {
int num,arr[100];
int i, y, swap;
num = 25;
printf("Enter %d integers \n",num);
for(i = 0; i < 25; i++) {
printf("\nElement %d: ", i + 1);
scanf("%d", &arr[i]);
}
for(i = 0; i < ( num - 1 ); i++) {
for(y = 0 ; y < num - i - 1; y++) {
if(arr[y] > arr[y+1]) {
swap = arr[y];
arr[y] = arr[y+1];
arr[y+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for ( i = 0 ; i < num ; i++ )
printf("%d \t", arr[i]);
getch();
}
I am expecting my output will be like this.
Sorted list in ascending order:
x x x x x
x x x x x
x x x x x
x x x x x
x x x x x
Like this:
...
printf("Sorted list in ascending order:\n");
for ( i = 0 ; i < 5; i++ ) {
printf("%c%d", (i % 5) ? '\t' : '\n', arr[i]);
}
printf("\n");
...
Or, after "chux", without the leading newline:
...
printf("Sorted list in ascending order:\n");
for ( i = 0 ; i < num ; i++ ) {
printf("%d%c", arr[i], ((i % 5) != (5-1)) ? '\t' : '\n');
}
...
And, if you want the columns nicely aligned, add an appropriate width to the %d:
printf("%10d ... "); /* right aligned */
or
printf("%-10d ... "); /* left aligned */
Just one modification:
printf("Sorted list in ascending order:\n");
for ( i = 0 ; i < num ; i++ )
{
printf("%d \t", arr[i]);
if(i % 5 == 0) putchar('\n');
}
Avoid printing extra space-tab after the last number.
To get visually lined up values, use a print width.
int print_width = 4;
int columns = 5;
for (i = 0 ; i < num ; i++ ) {
printf("%*d", print_width, arr[i]);
putchar(i%columns == columns-1 ? '\n' : '\t');
}

Using for loop to print out output

My for loops seems not working properly. First number which tells me how many task do I want my program to do is n. When I input 1 or 2, it works, but when I input 3 and more, it starts to struggle. Every row has 3 numbers separated by a space as you can see in the code. I am not getting all outputs. Algorithm in this program works perfect so no problem there.
Please ignore comments in the code. And sorry for my english.
#include <stdio.h>
int n;
int i;
int s;
int d;
int p;
int k;
int A;
int x;
int r[];
int koniec;
main()
{
scanf("%d", &n);
while( !(n >= 1 && n <=1000) )
{
//printf ( "max 1000 uloh min 1 \n");
return 1;//scanf("%d", &n);
}
for( i=1; i < n; i++)
{
scanf("%d %d %d", &s, &p, &d);
while((s < 1) || (s > 15000) || (p < 1) || (p > 4000) || (d < 1) || (d > 15000)) {
//printf ("prekroceny limit \n");
return 1; // scanf("%d %d %d", &s, &p, &d);
}
k = s / d;
A = s - ( k * d );
if ( A == 0 )
{
r[i] == 0;
}
else
{
//s = k * d + A ;
x = ( A * p ) / d ;
r[i] = p - x ;
}
}
for ( koniec = 0 ; koniec < i+1 ; koniec++ )
{
printf ( "%d", r[koniec] ) ;
printf ( "\n");
}
system("pause");
}
Example input so you can understand better:
4
5 4 4
6 100 3
500 5 1000
314 159 26
and output:
3
0
3
147
EDIT>
5
3434 234 2345
14455 345 12
134 145 1345
9242 2455 13455
83 34 133
output:
126
144
141
769
13
or something shorter>
input:
2
15000 1 1
1 4000 1
output:
0
0
Im getting return value 1 in both examples
My final code edit:
#include <stdio.h>
#include <stdlib.h>
int n;
int i;
int s;
int d;
int p;
int k;
int A;
int x;
int *r;
int koniec;
main()
{
scanf("%d", &n);
while( !(n >= 1 && n <=1000) )
{
return 0;//printf ( "max 1000 uloh min 1 \n");
//scanf("%d", &n);
}
r = (int *)malloc(n*sizeof(int));
for(i=0; i < n; i++)
{
scanf("%d %d %d", &s, &p, &d);
while((s < 1) || (s > 15000) || (p < 1) || (p > 4000) || (d < 1) || (d > 15000))
{
//printf ("prekroceny limit \n");
return 0; //scanf("%d %d %d", &s, &p, &d);
}
k = s / d;
A = s - ( k * d );
if ( A == 0 )
{
r[i] = 0;
}
else
{
//s = k * d + A ;
x = ( A * p ) / d ;
r[i] = p - x ;
}
}
for ( koniec = 0 ; koniec < n ; koniec++ )
{
printf ( "%d", r[koniec] ) ;
printf ( "\n");
}
free(r);
return 0;
}
You need to use malloc() to allocate memory dynamically. (Also #include stdlib.h)
For instance, add the following line after the first while loop :
//Note - at the top, change int r[] to the below:
int *r; //Instead of int r[]
//End note
while( !(n >= 1 && n <=1000) )
{
printf ( "max 1000 uloh min 1 \n");
scanf("%d", &n);
}
r = (int *) malloc(n*sizeof(int)); //Allocate "n" integers to your 'array r'
It should also be noted that once you are done using the array r, you must free() this memory like so:
free(r);
In addition, there are some other coding problems:
Be sure for( i=1; i < n; i++) is what you want. You use "i" to index your array, but arrays in C start from 0. So it (likely) should read for( i=0; i < n; i++).
r[i] == 0; is used falsely as an assignment. Use a single = to assign a value to a variable. Use the double equals == to compare two values.
for ( koniec = 0 ; koniec < i+1 ; koniec++ ) will likely through an error. Your i will be one less than n from the for-loop above it. So trying to iterate to i+1 or n will result in an array out of bounds problem -- aka a Segmentation Fault.
Edit - This should do what you want:
#include <stdio.h>
#include <stdlib.h>
int n;
int i;
int s;
int d;
int p;
int k;
int A;
int x;
int *r;
int koniec;
main()
{
scanf("%d", &n);
while( !(n >= 1 && n <=1000) )
{
//printf ( "max 1000 uloh min 1 \n");
//scanf("%d", &n);
return 1;
}
r = (int *)malloc(n*sizeof(int));
for(i=0; i < n; i++)
{
scanf("%d %d %d", &s, &p, &d);
while((s < 1) || (s > 15000) || (p < 1) || (p > 4000) || (d < 1) || (d > 15000))
{
//printf ("prekroceny limit \n");
//scanf("%d %d %d", &s, &p, &d);
return 1;
}
k = s / d;
A = s - ( k * d );
if ( A == 0 )
{
r[i] = 0;
}
else
{
s = k * d + A ; //This line does nothing... Just so you know
x = ( A * p ) / d ;
r[i] = p - x ;
}
}
for ( koniec = 0 ; koniec < n ; koniec++ )
{
printf ( "%d", r[koniec] ) ;
printf ( "\n");
}
free(r);
system("pause");
return 0;
}
Input:
4
5 4 4
6 100 3
500 5 1000
314 159 26
Output:
3
0
3
147
Allocate space for r[].
Change assignment.
Change for loop range.
//int r[];
main() {
scanf("%d", &n);
// while( !(n >= 1 && n <=1000) )
if (!(n >= 1 && n <=1000) ) {
return 1;//scanf("%d", &n);
}
int r[n]; // allocate
// for( i=1; i < n; i++)
for( i=0; i < n; i++) // Change range (#MrHappyAsthma)
...
// r[i] == 0;
r[i] = 0; // change
r[i] = p - x ;
// for ( koniec = 0 ; koniec < i+1 ; koniec++ )
for ( koniec = 0 ; koniec < n ; koniec++ )

Resources