This program is for adding two polynomials and printing the result.
It just stores the multipliers in array and use the index az the power of parameter
It does sum and subtract but when it comes to product it prints
0x0 0*x2 0*x3 ...
It's for basic C programming class and in three hours I should give it to master :-(
It gets the multiplier in an array and calculates the result using some functions:
#include <stdio.h>
#include <stdlib.h>
double a[50], b[50], c[101];
int dega, degb;
SumArray (a, b)
double a[], b[];
{
extern int dega, degb;
extern double c[];
int i, max = (dega < degb ? degb : dega) + 1;
for(i = 0; i < max; i++)
c[i] = a[i] + b[i];
}
SubtractArray(a, b)
double a[], b[];
{
extern int dega, degb;
extern double c[];
int i, max = (dega < degb ? degb : dega) + 1;
for (i = 0; i < max; i++)
c[i]=a[i]-b[i];
}
ProductArray(a, b)
double a[], b[];
{
extern int dega, degb;
extern double c[];
int i,j;
double tempa, tempb;
for(i = 0; i < dega + 1; i++)
for(j = 0; j < degb + 1; j++)
{
tempa = a[i];
tempb = b[j];
c[i + j] = c[i + j] + (tempa * tempb);
}
}
int main()
{
extern int dega, degb;
extern double a[50], b[50], c[]; //stores the multipliers
int i, operation;
for(i = 0; i < 50; i++)
a[i] = b[i] = 0;
for(i = 0; i < 102; i++)
c[i] = 0;
printf("darjeye chand jomleee ha ra vared konid");
scanf("%d %d", °a, °b);
printf("zarayebe chand jomlee aval ra vaerd konid");
for(i = 0; i < dega + 1; i++)
scanf("%d", &a[i]);
printf("zarayebe chand jomlee dovoom ra vaerd konid");
for(i = 0; i < degb + 1; i++)
scanf("%d", &b[i]);
printf("amaliyate morede nazartan ra vare konid baraye jame 0, tafrigh 1, zarb 2 ra vared konid");
scanf("%d", &operation);
switch(operation)
{
case 0:
{
SumArray(a,b);
for (i = 0; i < (dega > degb ? dega : degb) + 1; i++)
{
printf(" %d*x", c[i]);
printf("%d ", i);
}
break;
}
case 1:
{
SubtractArray(a,b);
for (i = 0; i < (dega > degb ? dega : degb) + 1; i++)
{
printf(" %d*x", c[i]);
printf("%d ", i);
}
break;
}
case 2:
{
ProductArray(a,b);
for(i = 0; i < (dega + degb + 1); i++)
{
printf("\%d*x", c[i]);
printf("%d ", i);
}
break;
}
default:
printf("amaliyate vared shode sahih nabud");
}
system("PAUSE");
return 0;
}
scanf("%d",a[i]); should be scanf("%d",&a[i]); and scanf("%d",b[i]); should be scanf("%d",&b[i]);
scanf requires address of the variable. For an array, say, in your case double a[50], simply writing a gives you the starting address of array a which will be same as &a[0] where a[0] is simply the first element, but not the address to the first element. a[i] is the element where &a[i] is the address to that element. Hope you got it.
Related
I need to multiply two square matrixes A and B 15x15.
Unfortunately, I'm getting this kind of error.
I know the problem is in pointers while calculating matrix C.
C[i][j] += *(A + k) * *(B + k)
I hope you can explain me what's wrong. I'm a beginner xD.
Thank you in advance.
#include <stdio.h>
#define N 15
#define _CRT_SECURE_NO_WARNINGS
int main() {
int A[N][N];
int B[N][N];
int C[N][N];
printf("Input matrix A.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("Enter your element:\n");
scanf_s("%d", &A[i][j]);
}
printf("\n");
}
printf("Input matrix B.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("Enter your element:\n");
scanf_s("%d", &B[i][j]);
}
printf("\n");
}
printf("Matrix A.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", A[i][j]);
}
printf("\n");
}
printf("Matrix B.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", B[i][j]);
}
printf("\n");
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
C[i][j] = 0;
for (int k = 0; k < 14; k++) {
C[i][j] += *(A + k) * *(B + k);
k++;
}
}
}
printf("Your result:\n");
printf("Matrix C.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
The problem in the multiplication is that A+k and B+k have type int (*)[15] which means dereferencing it once only makes a pointer out of them; furthermore, you need to take row and column items individually, which means A[i][k] and B[k][j], right? (also, there's no point on using confusing syntax, as the underlying operation is exactly the same).
Here's a fixed and improved version:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 15
/* Improvement 1 (type abstraction) */
typedef int NxN_int_matrix[N][N];
/* Improvement 2 (input function & wrapper) */
#define input_matrix(var) input_matrix_ex((var), #var)
static void input_matrix_ex(NxN_int_matrix dst, char *name)
{
printf("Input matrix %s.\n", name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
/* Improvement 3 (nicer prompt) */
printf("%s[%2d][%2d]: ", name, i, j);
fflush(stdout);
scanf_s("%d", &dst[i][j]);
}
}
printf("\n");
}
/* Improvement 4 (print function) */
#define print_matrix(var) print_matrix_ex(#var, (var))
static void print_matrix_ex(char *name, NxN_int_matrix M)
{
printf("Matrix %s.\n", name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", M[i][j]);
}
printf("\n");
}
}
/* Improvement 5 (move multiplication to a function too, and fix it) */
static void mult_matrix(NxN_int_matrix dst, NxN_int_matrix a, NxN_int_matrix b)
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
/* Improvement 6 (don't write out intermediate values) */
int tmp = 0;
for (int k = 0; k < N; k++)
tmp += a[i][k] * b[k][j];
dst[i][j] = tmp;
}
}
}
int main()
{
NxN_int_matrix A, B, C;
input_matrix(A);
input_matrix(B);
print_matrix(A);
print_matrix(B);
mult_matrix(C, A, B);
printf("Your result:\n");
print_matrix(C);
return 0;
}
/* Possible further improvements:
* - using a transposed B might make multiplication faster
*/
I am trying to raise a matrix to a power, using pointers but there is a mistake in my code I can't find.
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
>
int **alloc(int r, int c) {
int **d;
d = (int **)malloc(r * sizeof(int *));
for (int i = 0; i < r; i++) {
d[i] = (int *)malloc(c * sizeof(int));
}
return d;
void input(int **A, int r, int c) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("[%d][%d]=", i, j);
scanf_s("%d", &A[i][j]);
}
}
}
void output(int **A, int r, int c) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
printf("%d ", A[i][j]);
}
printf("\n");
}
}
void power(int **A,int**D, int r, int c,int p) {
int i, j,k;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
D[i][j] = A[i][j];
}
}
while (p) {
// this is the matrix multiplication, where I attempt to multiply my matrix A with itself, and store the result in D, which initially started as A's copy, and p is the power I'm raising it to.
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
for (k = 0; k < c; k++)
D[i][j] = D[i][j] + A[i][k] * D[k][j];
}
}
p--;
}
}
void main() {
int r, c;
int **A, **D;
printf("rows A: ");
scanf_s("%d", &r);
printf("columns A: ");
scanf_s("%d", &c);
A = alloc(r, c);
printf("\nValues of A:\n");
input(A, r, c);
printf("\nMatrIX A is:\n");
output(A, r, c);
D = alloc(r, c);
printf("input the value you want to raise your matrix to: ");
int p;
scanf_s("%d", &p);
power(A, D, r, c, p);
printf("\nMatrix raised to said power is:\n");
output(D, r, c);
_getch();
}
When I input the rows and columns as 2 each, and input all values in the matrix as 1 and raise it to the power of 3, my answer should be
4 4
4 4
instead of
72 72
232 232
What is wrong in my code? If I were to print the D matrix before the multiplication, it would print it correctly, as:
1 1
1 1
Your two-step allocation looks okay, except that you don't free the memory after you're done. Your problem is in how you multiply the matrix:
Raising a matrix to a power involves multiplying it to itself. You can only do that if the matrix is square. You can replace all occurrences of rows r and columns c with a single dimension n.
When you do the actual multiplication:
D[i][j] = D[i][j] + A[i][k] * D[k][j];
you assign to D and read from it at the same time. Subsequent calculations (of the same multiplication) will see a changed value of D[i][j]. You will need a temporary "scratch" matrix.
Your code multiplies once too many. Also, Raising a matrix to the power of zero should yield the identity matrix.
Here's how your power function could look like:
void power(int **A, int **D, int n, int p)
{
int i, j,k;
// assign identity matrix to result D
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
D[i][j] = (i == j);
}
}
// create a scratch matrix
int **tmp = alloc(n);
while (p-- > 0) {
// multiply [tmp] = [A] * [D]
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
tmp[i][j] = 0;
for (k = 0; k < n; k++)
tmp[i][j] += A[i][k] * D[k][j];
}
}
// copy [D] = [tmp]
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
D[i][j] = tmp[i][j];
}
}
}
// TODO: clean up the scratch matrix
}
in function calcdist() program stops running sometimes.
is it my computer or my code?
The program takes size for a 2d array (a square parking slot) then take the cars, after that finds best slot for our car.
best slot must have the largest distance to the nearest point.
distance is calculating by using manhatten distance.
most of the printf s for finding where the problem is.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define CAPACITY 50
int calcdist(int x, int y, int carnum[][CAPACITY], int size);
int main ()
{
int size, numofcars, x, y, mindist = 0, bestx = 0, besty = 0;
printf("Size: ");
scanf("%d", &size);
int carnum[size][size];
printf("Cars: ");
scanf("%d", &numofcars);
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
carnum[i][j] = 0;
}
}
for(int i = 0; i < numofcars; i++){
printf("Locations: ");
scanf("%d %d", &x, &y);
carnum[x - 1][y - 1] = 1;
}
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
printf("%d ",carnum[i][j]);
}
printf("\n");
}
printf("carnum[3][0]: %d\n",carnum[3][0]);
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
if(carnum[i][j] == 0){
printf("hi ");
if(mindist < calcdist(i, j, carnum, size)){
mindist = calcdist(i, j, carnum, size);
bestx = i + 1;
besty = j + 1;
printf("mindist is %d\n", calcdist(i, j, carnum, size));
}
}
}
}
printf("Best Slot Found In: %d %d\n", bestx, besty);
return 0;
}
int calcdist(int x, int y, int carnum[][CAPACITY], int size){
int dist = size * 2;
printf("x is: %d y is : %d\n", x, y);
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
printf("i is: %d j is : %d ", i, j);
if((carnum[i][j] == 1) && (dist > (abs(x - i) + abs(y - j) ))){
dist = abs(x - i) + abs(y - j);
}
printf("i is: %d j is : %d\n", i, j);
}
}
return dist;
}
/****** && (logical and) not & (unary operator) ******/
if((carnum[i][j] == 1) && (dist > (abs(x - i) + abs(y - j) )))
Programming in C for finding maximum in 2D array using dynamic memory allocation.
int main() {
int i, arr[m][n], j, m, n;
int max = 0;
int *ptr;
printf("enter the value m");
scanf("%d", m);
printf("enter the vaue of n");
scanf("%d", n);
ptr = (int*) malloc(m * n * 4);
printf("enter the values\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", ((ptr + i * n) + j));
}
}
max = arr[0][0];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (max < *((ptr + i * n) + j));
max = *((ptr + i * n) + j);
}
}
printf("%d", max);
}
I made changes to your code to remove the error you are getting. I commented the changes that I have made.
int main()
{
/*int i, arr[][], j, m, n;*/
/* Because arr is allocated dynamically, you have to declare as a pointer to int. */
int i, *arr, j, m, n;
int max = 0;
int *ptr;
printf("enter the value m");
scanf("%d", m);
printf("enter the vaue of n");
scanf("%d", n);
/*ptr = (int*)malloc(m * n * 4);*/
/* It is better to use sizeof(int) because int does not have the same length of all computers. */
ptr = (int*)malloc(m * n * sizeof(int));
printf("enter the values\n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", ((ptr + i * n) + j));
}
}
/*max = arr[0];*/
/* To get the first int at arr, you could also use arr[0], but not arr[0][0] because */
/* this would require that arr be an array of pointers to array of int's, which is not the case. */
max = *arr;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
if (max < *((ptr + i * n) + j));
max = *((ptr + i * n) + j);
}
}
printf("%d", max);
}
You must learn the algorithms and programming language C.
So you can find some courses in this site :
learn-c
try this code is functioned:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[]) {
int i, j, m, n;
int max;
int **ptr;
printf("enter the value m: ");
scanf("%d", &m);
printf("enter the vaue of n: ");
scanf("%d", &n);
ptr = (int **)malloc(n * sizeof(int *));
for (i = 0; i < m; i++) {
*(ptr + i) = (int *)malloc(m * sizeof(int));
for (j = 0; j < n; j++) {
scanf("%d", (*(ptr + i) + j));
}
}
max = **ptr;
printf("\n\nMatrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", *(*(ptr + i) + j));
if (max < *(*(ptr + i) + j))
max = *(*(ptr + i) + j);
}
printf("\n");
}
printf("\nthe max is %d \n", max);
return 0;
}
I've presented the codes below only while executing the first loop it works fine but as soon as i uncomment the second loop it starts to throw segmentation fault. My code is as below.
// Write a program to add two m*n matrices using pointer.
#include <stdio.h>
#define m 2
#define n 2
int main() {
int (*a)[n];
int (*b)[n], i, j; //, *(sum)[n], i, j;
printf("Enter first matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", *(a + i) + j);
}
}
printf("Enter second matrix:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", *(b + i) + j);
}
}
// printf("The Sum of matrix is:\n");
// for (i = 0; i < m; i++) {
// for (j = 0; j < n; j++) {
// // *(*(sum + i) + j) = *(*(a + i) + j) + *(*(b + i) + j);
// // printf("\t%d", *(*(sum + i) + j));
// }
// printf("\n");
// }
}
You are not defining a and b as 2D arrays, but as uninitialized pointers to 2D arrays. passing addresses into these invokes undefined behavior. You must make these pointers point to an actual array, either static, automatic or allocated from the heap.
You can define 2D arrays this way:
int a[m][n], b[m][n];
If you are required to use pointers, you can allocate the 2D arrays with malloc:
int (*a)[n] = malloc(sizeof(*a) * m);
int (*b)[n] = malloc(sizeof(*b) * m);
In your program, it is more readable to use the [] syntax, even for pointers:
#include <stdio.h>
#include <stdlib.h>
#define m 2
#define n 2
int main(void) {
int (*a)[n] = malloc(sizeof(*a) * m);
int (*b)[n] = malloc(sizeof(*b) * m);
int (*sum)[n] = malloc(sizeof(*sum) * m);
printf("Enter first matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter second matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &b[i][j]);
}
}
printf("The Sum of matrices is:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
sum[i][j] = a[i][j] + b[i][j];
printf("\t%d", sum[i][j]);
}
printf("\n");
}
return 0;
}