c program stops executing without any errors - c

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) )))

Related

Error in a C program using dynamic memory allocation and array

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;
}

Function to solve a system using Gauss-Seidel

I'm trying to write a function in order to solve a system of equations using the Gauss-Seidel method. The function I have so far doesn't do the job. The code stops giving me output after "Enter an initial guess..." and doesn't solve the function I'm inputting. Is this an issue with my function or some other part of my code?
/* code to solve a nxn system using the Gauss-Seidel method */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAX_DIM 100
#define MAX_ITER 500
#define TOLERANCE 1.e-6
void gauss_seidel(double a[][MAX_DIM], double b[], double x[], int n);
void main()
{
int i, j, n;
int violation_counter, answer;
int violation_rows[MAX_DIM];
double sum;
double a[MAX_DIM][MAX_DIM];
double b[MAX_DIM], x[MAX_DIM];
/* read in data */
n = MAX_DIM + 1;
while (n > MAX_DIM) {
printf("Enter the dimension of the system to be solved: ");
scanf_s("%d", &n);
}
printf("\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Enter a[%d][%d] element of the system matrix: ",i,j);
scanf_s("%lf", &a[i][j]);
}
printf("Enter b[%d] of the right-hand-side vector: ", i);
scanf_s("%lf", &b[i]);
printf("\n");
}
/* test the convergence criterion */
violation_counter = 0;
for (i = 0; i < n; i++) {
sum = 0.0;
for (j = 0; j < n; j++)
if (i != j)
sum = sum + fabs(a[i][j]);
if (fabs(a[i][i]) < sum) {
violation_rows[violation_counter] = i;
violation_counter = violation_counter + 1;
}
if (a[i][i] == 0.0) {
printf("Found diagonal element equal to zero; rearrange equations; exiting ...\n");
exit(0);
}
}
if (violation_counter > 0) {
printf("The Gauss-Seidel convergence criterion is violated in %d rows out of %d\n", violation_counter, n);
printf("Specifically, it was violated in rows:\n");
for (i = 0; i < violation_counter; i++)
printf("%d ", violation_rows[i]);
printf("\n");
printf("Enter 1 if you want to continue; any other number to abort : ");
scanf_s("%d", &answer);
if (answer != 1)
exit(1);
printf("Check results carefully\n\n");
}
/* initialize the solution vector -- initial guesses */
for (i = 0; i < n; i++) {
printf("Enter an initial guess for x[%d] of the solution vector : ",i);
scanf_s("%lf", &x[i]);
}
/* solve the system */
gauss_seidel(a, b, x, n);
/* output solution */
for (i = 0; i < n; i++)
printf("x[%d]=%f\n", i, x[i]);
printf("\n");
}
/* function to solve a system using Gauss-Seidel */
void gauss_seidel(double a[][MAX_DIM], double b[], double x[], int n)
{
double maxerror = 1.0;
double iteration_error;
double e, sum, temp;
int i, j;
while (maxerror > 1.e-6) {
iteration_error = 0.0;
for (i = 0; i < n; i++) {
sum = 0.0;
for (j = 0; j < n; j++) {
if (i != j)
sum = sum + (a[i][j] * x[j]);
}
}
temp = (a[i][n] - sum) / a[i][i];
e = fabs((temp - x[i]) / x[i]);
x[i] = temp;
if (e > iteration_error)
iteration_error = e;
}
maxerror = iteration_error;
}

Newtons divided difference

The problem is this http://www.spoj.com/problems/CMPLS/
I thought of implementing newtons divided difference and find out the polynomial. But I am not able to get the correct answer. Some basic test cases are passing but some are not.
# include <stdio.h>
int main()
{
int i, j, k, t, m, x[100][100], s, c, n;
long ans, mul;
scanf ("%d", &t);
while (t--) {
scanf ("%d%d", &s, &c);
n = s;
for (i = 0; i < n; i++)
scanf ("%d", &x[i][0]);
for (j = 1; j < n; j++)
for (i = 0; i < n; i++) {
x[i][j] = x[i+1][j-1] - x[i][j-1];
}
k = n;
printf("\n");
for (i = 0; i < n; i++) {
for(j = 0; j < k; j++)
printf ("%d ", x[i][j]);
printf ("\n");
k--;
}
printf ("\n\n");
for (m = 1; m <= c; m++) {
ans = x[0][0];
for ( i = 1; i < n - 1; i++) {
mul = x[0][i];
for (j = 1; j <= i; j++)
mul = mul * (s + m - j);
ans = ans + mul;
}
printf ("%ld ", ans);
}
printf ("\n");
}
return 0;
}

Declaring a function in C gives error for expected ';', ',' or ')'

#include <stdio.h>
#include <stdlib.h>
int second_smallest(int &A[m][n], m, n);
int main()
{
int m, n;
do{
scanf("%d %d", &m, &n);
}while(((m < 1) || (m > 5)) && ((n < 1) || (n > 5)));
int A[m][n], i, j;
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
scanf("%d", &A[i][j]);
printf("Second smallest number in the two dimensional array is %d", second_smallest(A[m][n], m, n));
return 0;
}
int second_smallest(int &A[m][n], m, n){
int i, j, min, second_min;
if (A[0][0] > A[0][1]){
min = A[0][0];
second_min = A[0][1];
}
else{
min = A[0][1];
second_min = A[0][0];
}
for (int i = 0;i < m; i++)
for (int j = 0; j < n; j++){
if (A[i][j] <= min)
second_min = min;
min = A[i][j];
}
else if (A[i][j] > second_min){
second_min = A[i][j];
}
}
I'm writing a function to find the second smallest number in a two dimensional array and I'm having a problem with declaring the function.
It gives me an error to the following lines:
int second_smallest(int &A[m][n], m, n);
and
int second_smallest(int &A[m][n], m, n){
The error says the following:
error: expected ';', ',', or ')' before '&' token.
And one warning for this line:
printf("Second smallest number in the two dimensional array is %d", second_smallest(A[m][n], m, n));
Saying:
warning: implicit declaration of function 'second_smallest'
#define m 4
#define n 3
int second_smallest(int &A[m][n], m, n);
// WHAT IS M, N ? IN C YOU HAVE TO DECLARE
// STATIC ARRAY SIZE BEFORE COMPILATION
// DECLARE THEM AS MACROS
int main()
{
int m, n;
do{
scanf("%d %d", &m, &n);
}while(((m < 1) || (m > 5)) && ((n < 1) || (n > 5)));
int A[m][n], i, j;
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
scanf("%d", &A[i][j]);
printf("Second smallest number in the two dimensional array is %d", second_smallest(A[m][n], m, n));
return 0;
}
int second_smallest(int &A[m][n], m, n){
int i, j, min, second_min;
if (A[0][0] > A[0][1]){
min = A[0][0];
second_min = A[0][1];
}
else{
min = A[0][1];
second_min = A[0][0];
}
for (int i = 0;i < m; i++) // SOME BRACKETS MISMATCH AFTER THIS , CHECK IT.
for (int j = 0; j < n; j++){
if (A[i][j] <= min)
second_min = min;
min = A[i][j];
}
else if (A[i][j] > second_min){
second_min = A[i][j];
}
}
1. Declare size of array before using them anywhere, if you dont know size, take a pointer as a function parameter and calculate its size using sizeof() operator.
2.Check brackets in your code; The error you have is of a mismatching number of brackets.
First problem with your function prototype is parameter &A[m][n]. C doesn't allow pass by reference. Your function prototype is wrong
int second_smallest(int &A[m][n], m, n);
C doesn't allow this type of function prototype.
Another problem is m and n are not known to compiler when it encounter with A[m][n].
Change it to
int second_smallest(int m, int n,int A[m][n]);
After that change the code snippet
for (int i = 0;i < m; i++)
for (int j = 0; j < n; j++){
if (A[i][j] <= min)
second_min = min;
min = A[i][j];
}
else if (A[i][j] > second_min){
second_min = A[i][j];
}
to
for (int i = 0;i < m; i++){
for (int j = 0; j < n; j++){
if (A[i][j] <= min){
second_min = min;
min = A[i][j];
}
else if (A[i][j] > second_min){
second_min = A[i][j];
}
}
}

C run-time error for a program to sum polynomials

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", &dega, &degb);
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.

Resources