I have been trying to make this code for multidimensional matrices multiplication to work without much success for a couple of weeks by now.
The code works for any given multiplication between matrices of the same dimensions, however when I run something like 2x3*3x4 it only stores about half of the values in the resultant matrix, I have gone through many iterations of the programs including complete rewrites from scratch or working from a previous program that would do the same but without pointer notation and working. It is for an assignment, so I have the limitations of using the pointer notation and not using dynamic memory. I am not sure what I'am missing here even after going through documentation and examples. can you guys give me some suggestions of what is happing here?.
#include<stdio.h>
int main(){
int i, j, k, l, n1=1, n2=1, n3=1, n4=1, m1;
int p1[11][11], p2[11][11], p3[11][11];
printf("\n\nInput first matrix number of rows and columns : \n");
scanf("%d %d", &n1, &n2);
printf("\n\nInput second matrix number of rows and columns : \n");
scanf("%d %d", &n3, &n4);
printf("\n\nInput your values for the first matrix: \n");
for(i=0; i<n1; i++){
for(j=0; j<n2; j++){
printf("\n Input element of Row [%d] and Colunm[%d] = ", i+1, j+1);
scanf("%d", (*(p1+i)+j));
}
}
printf("\n\nInput your values for the second matrix : \n");
for(i=0; i<n3; i++){
for(j=0; j<n4; j++){
printf("\n Input element of Row [%d] and Colunm[%d] = ", i+1, j+1);
scanf("%d", (*(p2+i)+j));
}
}
printf("\n\n\n");
for(i=0;i<n1;i++){
for(j=0;j<n4;j++){
m1=0;
for(k=0;k<n3;k++){
m1+=(*(*(p1 + i) + k)) * (*(*(p2 + k) + j));
}
*(*(p3+j)+i)=m1;
printf("[%d][%d][%d][%d] ", *(*(p1+i)+j), *(*(p2+i)+j), *(*(p3+i)+j), m1);
}
}
printf("\n\n");
printf("\n\nThe result is: \n\n\n\t");
for(i=0; i<n1; i++){
printf("\n\t");
for(j=0; j<n4; j++){
printf("[%d]\t", *(*(p3+i)+j));
}
printf("\t\n\n\n\t");
}
printf("\n");
system("pause");
}
I don't get messages errors but for a given matrix multiplication of different dimensions 2x3*3x4 the result is:
12 12 0 0
12 12 9 2508544
or similar while the expected output is:
12 12 12 12
12 12 12 12
same applies for any matrices that have different dimensions, but square matrices come out alright.
In your calculation loop, you address *(*(p3 + j) + i), using the inner for loop variable first.
In your print loop, you use *(*(p3 + i) + j), using your outer for loop variable first.
So your print loop prints on the transpose of your result matrix.
Related
I hope someone can help me.
I want to create stars "*" instead of my numbers in the output of array, 10 numbers.
Example
Input
Number 1: 3
Number 2: 2
Number 10: 5
Output
Number 1: ***
Number 2: **
Number 10: *****
But i get always the same number of stars.
Thanks! Dear Harman
#include <stdio.h>
int main()
{
int tal[10], i, j, z;
printf("Mata in tio tal mellan 0 och 60:\n");
for(z=0;z<10;z++)
{
printf("Tal %d: ", z+1);
scanf("%d",&tal[i]);
}
printf("\nRita ut stapeldiagram:\n");
for(z=0;z<10;z++)
{
printf("Tal %d: ", z + 1);
for (j=0; j<i; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Here's your bug. Your inner for-loop that prints a star is redefining the iteration index, i. Simple fix is to use a new variable, say j, in this line:
for (i=0; i<tal; i++)
To this:
for (int j=0; j<tal; j++)
First of all, when scanning user input you are storing the value at the same position of the array tal . Increment the value of i so that in every iteration the new value takes a new memory place .
for(z=0, i=0; z<10; z++, i++)
{
printf("Tal %d: ", z+1);
scanf("%d",&tal[i]);
}
Secondly when printing out the stars in the inner loop, you are iterating to a fixed number i . So , it is printing the fixed number of stars in every iteration of outer loop. Iterate the inner loop to tal[z] . I hope this will make you smile .
for(z=0;z<10;z++)
{
printf("Tal %d: ", z + 1);
for (j=0; j<tal[z]; j++)
{
printf("*");
}
printf("\n");
}
I'm a beginner to C, and am trying to sort user inputted numbers into odd and even arrays. I don't understand why my code isn't working.
Cheers.
This is my code, I don't understand my mistake.
int x[]= {};
int i=0;
int d=0;
int j=0;
int even[12]={};
int odd[12]={};
printf("Enter amount of numbers: "); // asking user for amount of numbers
scanf("%d", &d);
for (j=0; j<d; j++){
printf("Enter number %d: ", i+1); // scanning input into 'x' array
scanf("%d", x[i]);
}
printf("Even numbers: ");
for (i=0; i<d; i++) {
if (x[i] % 2 == 0) { // sorting into even array
even[i]=x[i];
printf("%d \n", even[i]);
}
}
printf("\n Odd numbers: ");
for (i=0; i<d;i++){
if (x[i] % 2 != 0) { // sorting into odd array
odd[i]=x[i];
printf("%d \n", odd[i]);
}
}
This error message keeps coming up:
$ ./main
Enter amount of numbers: 4
Enter number 1: 6
Segmentation fault (core dumped)
int x[]= {}; doesn't work because it would hold no elements. But initializing it with {} doesn't work in C anyway, do this instead:
int x[24] = {0}; // first element explicitely set to 0, the rest default-initialized to 0
You also need to put {0} for even and odd. If it's compiling for you with {} then it's possible that you're compiling it as a C++ program, or perhaps your compiler just tolerates it anyway (but it won't work on every C compiler).
scanf needs the address of the int, so instead of scanf("%d", x[i]); you need scanf("%d", &x[i]);. But i is the wrong iterator for this for (j = 0; j < d; j++) loop. Instead do this:
for (j = 0; j < d; j++) {
printf("Enter number %d: ", j + 1); // scanning input into 'x' array
scanf("%d", &x[j]);
}
Also note that the way you're doing this, half the array will be left at 0. So for instance if I imputted the values 1 through 6, then odd contains the values 1 0 3 0 5 0.
I'm trying to print the sum of columns 1, 4 and 6.
When I input 30 one's into my 2d array this is what I get as result:
Sum of 1st column is: 6
Sum of 1st column is: 12
Sum of 1st column is: 18
Sum of 1st column is: 24
Sum of 1st column is: 30
Sum of 4th column: 3
Sum of 4th column: 6
Sum of 4th column: 9
Sum of 4th column: 12
Sum of 4th column: 15
Sum of 6th column is: 1
Sum of 6th column is: 2
Sum of 6th column is: 3
Sum of 6th column is: 4
Sum of 6th column is: 5
#include <stdio.h>
int main()
{
int i, j, sum1=0, sum2=0, sum3=0, arr[5][6];
int value;
for(i=0; i < 5; i++){
for(j=0; j<6; j++){
printf("Enter a value: ");
scanf("%d",&value);
arr[i][j]=value;
}
}
printf("Two Dimensional array elements:\n");
for(i=0; i<5; i++) {
for(j=0;j<6;j++) {
printf("%d ", arr[i][j]);
if(j==5){
printf("\n");
}
}
}
//this functions calculate the sum of the 1st column//
for(i=0; i<5; i++){
for(j=0; j<6; j++){
sum1 = arr[i][j] + sum1;
}
printf("Sum of 1st column is: %d\n", sum1);
}
//this functions calculate the sum of the 4th column//
for(i=0; i<5; i++){
for(j=3; j<6; j++){
sum2 = arr[i][j] + sum2;
}
printf("Sum of 4th column: %d\n", sum2);
}
//this functions calculate the sum of the 6th column//
for(i=0; i<5; i++){
for(j=5; j<6; j++){
sum3 = arr[i][j] + sum3;
}
printf("Sum of 6th column is: %d\n", sum3);
}
return 0;
}
Based on your printfs, I'm assuming you want to print the sums of columns 1, 4, and 6.
Let's just think about column 0 for simplicity - you can easily extend this.
You have a 2D array, and you want the sum of the numbers in column 0. Think about how you'd traverse your array iteratively to accomplish this. You want to keep your column fixed at 0, and just increment through your rows - does that make sense?
Here is one of many ways to do this:
//this functions calculate the sum of the 1st column//
for(i=0; i<5; i++){
sum1 = arr[i][0] + sum1;
}
printf("Sum of 1st column is: %d\n", sum1);
Does that make sense?
Basically, StoneThrow has already stated the cause, but there appears to be an error on the code regarding the loops, for example, in the first loop for adding the first column, you are actually adding the whole matrix since you're incrementing the number for the rows as well as for the column.
Code should go something like this:
//Addition for column one
for(i=0;i<5;i++){
sum1=arr[i][0]+sum1;
printf("Sum of 1st column is: %d\n", sum1);
}
//Addition for column four
for(i=0;i<5;i++){
sum2=arr[i][3]+sum2;
printf("Sum of the 4th column is: %d\n", sum2);
}
//Addition for column six
for(i=0;i<5;i++){
sum3=arr[i][5]+sum3;
printf("Sum of the 6th column is: %d\n", sum3);
}
So, just watch out with the looping, these don't need to be nested since you're actually just using one of the array's groups as a variable, and the other one as a constant.
I wanted to create a program to calculate the regression line of some given data, along with the errors, so I can use in my university assignments. This is the following program:
#include <stdio.h>
#include <math.h>
int main(void)
{
int i,n,N;
double x[n],y[n],a=0.0,b=0.0,c=0.0,d=0.0,D,P=0.0,p,Sx,A,B,dA,dB;
printf("Give the number of data you want to input for x : ");
scanf("\n%d", &N);
printf("\nGive the values of x : ");
for (i=1; i<=N; i++);
{
printf("\n Enter x[%d] = ", i);
scanf("%lf", &x[i]);
a+=x[i];
b+=pow(x[i],2);
}
printf("\nGive the values of y : ");
for (i=1; i<=N; i++);
{
printf("\n Enter y[%d] = ", i);
scanf("%lf", &y[i]);
c+=y[i];
}
D=N*b-pow(a,2);
A=(b*c-a*d)/D;
B=(N*d-a*c)/D;
for (i=1; i<=N; i++);
{
d+=x[i]*y[i];
p=y[i]-A-B*x[i];
P+=pow(p,2);
}
Sx=sqrt(P/N-2);
dA=Sx*sqrt(b/D);
dB=Sx*sqrt(N/D);
printf("\n x \t \t \t y");
for (i=1; i<=N; i++);
printf("\nx[%d] = %lf\t%lf = y[%d]", x[i],y[i]);
printf("\nA = %lf\t B = %lf", A,B);
printf("\nThe errors of A & B are dA = %lf and dB = %lf", dA,dB);
printf("\nThe equation of the regression line is y=%lfx+(%lf)", B,A);
return 0;
}
I have two problems.
Despite giving a value to N, the program runs so that I can only give one value for x and one value for y. Why and where is the mistake?
When printing the "Enter x[%d]", it displays x[11] and at the end when printing "x[%d] = %lf\t%lf = y[%d]", it displays x[0]. Again why and where is the mistake?
Thank you for your help!
You are trying to create a dynamic array in C.
To do that, you need to use dynamic memory allocation with malloc and free. So, your code should look something like this:
int N;
double *x;
printf("Give the number of data you want to input for x :\n");
scanf("%d", &N);
x = malloc(sizeof(double) * N);
Then, at the end of your program you need to free the memory:
free(x);
If you don't want to deal with manual memory management (or can't for some reason), you can use a static maximum array size like this:
#define MAX_N_X 100
int main(void) {
int N;
double x[MAX_N_X];
printf("Give the number of data you want to input for x :\n");
scanf("%d", &N);
if (N > MAX_N_X) {
printf("Can't handle that many inputs! Maximum %d\n", MAX_N_X);
return 0;
}
}
You simply missed two parameters to printf.
Yo wrote:
printf("\nx[%d] = %lf\t%lf = y[%d]", x[i],y[i]);
But it should be:
printf("\nx[%d] = %lf\t%lf = y[%d]", i, x[i], y[i], i);
I found the main problem with this program a few days after posting this and the fix would be removing the ; from the for commands, along with some other minor changes. I thought I might add this comment to let you know and now it is working like a charm. The simplest of mistakes fools even the trained eyes. After I found this, I was shocked that no one picked up on this mistake.
#include <stdio.h>
#include <conio.h>
void main()
{
int arr[5], new[5], i, j;
printf("ENTER ANY FIVE NUMBERS:");
scanf("%d%d%d%d%d", &arr[0], &arr[1], &arr[2], &arr[3], &arr[4]);
for(i=0; i<5; i++)
{
for(j=5; j>=0 ;--j)
{
new[i] = arr[j];
printf("%d", new[i]);
printf(" ");
}
}
getch();
}
The above code is of a simple problem, which asks to take inputs of numbers in an array and show the inverse array of the input. I tried to solve it myself and wrote the above code. But the compiler is showing the result multiple times, I mean, the result should have only 5 numbers but the output shows series of numbers.
Here is one problem:
for(j=5;j>=0;--j){
new[i]=arr[j];
^ out of bounds access to arr[5]
Change it to
for(j=4;j>=0;--j){ // 4 instead of 5
new[i]=arr[j];
That said, if all you want is to print an array in reverse order, simply do:
for(j=4;j>=0;--j){
printf("%d", arr[j]);
printf(" ");
}
printf("\n");
No need for two loops and no need for the extra array new
If you really want a "reversed copy" do:
for(j=4;j>=0;--j){
new[4-j] = arr[j];
printf("%d", arr[j]); // or printf("%d", new[4-j]);
printf(" ");
}
printf("\n");