Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I want to create a function fill_table to fill a table dynamically. The tail n of the table is declared in the main() function,
int n = 0;
float* *T;
void show_table(int n) {}
void fill_table(int n) {}
int main() {
printf(" Table dimension: ");
scanf("%d", &n);
fill_table(n);
show_table(n);
return 0;
}
I always get this error:
segmentation fault ./a.out
I try mes functions like that:
int n = 0;
float* *T=0;
void show_table(int n){
printf("Show Table: \n");
for (int i = 0; i < n; i++) {
printf("%f ", *(T + i));
}
}
void fill_table(int n) {
*T=(float*)calloc(n, sizeof(float));;
if (!T){
printf("Memoire not allowe\n");
exit(0);
} else {
for (int i = 0; i < n; i++) {
printf("\nT[%d]= ", i+1);
scanf("%f", (T + i));
}
}
}
After a lot of trial and error I found this solution:
must declare table as
float* T; //not float* *T;
function fill_table:
void fill_table(int n) {
T=(float*)calloc(n, sizeof(float));;
if (!T){
printf("Memoire not allowe\n");
exit(0);
} else {
for (int i = 0; i < n; i++) {
printf("\nT[%d]= ", i+1);
scanf("%f", (T + i));
}
}
}
function show_table:
void show_table(int n){
printf("Show Table: \n");
for (int i = 0; i < n; i++) {
printf("%f ", *(T + i));
}
}
Related
I need help. It doesnt display the printf in the mean function. I am doing a dynamic allocation in c and the add function works but the mean function does not display. There is no problem to the add function it works but the max does not. I am sorry I know this problem is simple but still cant get the answer. I am also getting a warning to the add function during the call in main.
This is my code:
typedef int* Statistician;
void add(Statistician answer, int *count, int *SIZE, int item);
|
[Note] expected 'Statistician' {aka 'int *'} but argument is of type 'int **'
int main() {
int SIZE;
Statistician *answer;
int count;
int item;
add(answer, count, SIZE, item);
|
//[Warning] passing argument 1 of 'add' from incompatible pointer type [-Wincompatible-pointer-types]
printf("\nThe mean is: %.2f", mean(answer, SIZE));
return 0;
}
This is the add function:
void add(Statistician answer, int *count, int *SIZE, int item) {
int i;
printf("Enter n: ");
scanf("%d", &item);
answer = (int*)malloc(item * sizeof(int));
if(item == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
for(i = 0; i < item; ++i) {
scanf("%d", &answer[i]);
}
printf("Elements of array are: ");
for(i = 0; i < item; i++) {
printf("%d ", answer[i]);
}
if(item == 10) {
int m;
printf("\nAppend array: ");
scanf("%d", &m);
answer = realloc(answer, m * sizeof(int));
for(i = item; i < item + m; i++) {
scanf("%d", &answer[i]);
}
item = item + m;
int temp, j;
for(i = 0; i < item; i++) {
for(j = 0; j <= i; j++) {
if(*(answer + i) < *(answer + j)) {
temp = *(answer + i);
*(answer + i) = *(answer + j);
*(answer + j) = temp;
}
}
}
printf("Final array: \n");
for(i = 0; i < item; ++i) {
printf("%d ", answer[i]);
}
}
}
}
This is the max function that doesnt display:
float mean(Statistician answer, int count) {
int mean =0;
int cnt = 0;
for(int i=0;i<count;i++){
mean = mean + answer[i];
cnt++;
}
mean = mean / cnt;
return mean;
}
I fully expected to find a duplicate but I didn't.
The first argument to add is declared to be int ** (via typedef) and you passed it a paramter of type int *. The compiler will let you do this with a warning, but it's almost always wrong. Don't do it.
If you're running 64 bit code, anything can happen after you stomp memory. 32 bit code is slightly more predictable but it's still going to end badly.
From your code, it looks like you want void add(Statistician *answer, Statistician answer; and add(&answer.
The deep learning of pointers is here. The thing you need to modify in the calling function is the thing whose address is passed to the called function. Allocating arrays with malloc almost always ends up being double pointers.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I've got a problem. I've tried to write program. This is command:
The user specifies a whole number n>0.
Program:
Allocates two arrays of numbers of type int size n+1
Using only these arrays and a small number of statically allocated variables, the program calculates recursively the n line of the Pascal triangle (all binomial symbols with an upper parameter equal to n)
Prints out the calculated values
Memory slowing down
Example
input: 5
output: 1 5 10 10 5 1
I wrote iteration, but I have no idea how change this for recursion.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n,i,k;
int * array_1;
int * array_2;
scanf("%d",&n);
if(n=='0') printf("%d", 1);
if(n=='1') printf("%d %d", 1, 1);
array_1 = (int*)calloc(n+1,sizeof(int));
array_2 = (int*)calloc(n+1,sizeof(int));
array_1[0] = 1;
array_1[1] = 1;
k=1;
while(k!=n)
{
for(i=0; i<=k+1; i++)
{
if(i==0)
{
array_2[0] = 1;
}
else if(i==n)
{
array_2[i] = 1;
}
else
{
array_2[i] = array_1[i] + array_1[i-1];
}
}
for(i=0; i<=n; i++)
{
array_1[i] = array_2[i];
array_2[i] = 0;
}
k++;
}
for(i=0; i<=n; i++)
{
printf("%d ", array_1[i]);
}
free(array_1);
free(array_2);
return 0;
}
The recursive version could look something like the following, with the actual work being left to fill-in under the two /* ... */ comments. The missing code essentially exists in the iterative version as posted, it just needs to be retrofitted here.
void recurse(int k, int n, int *array_1, int *array_2)
{
/*
print previously calculated k-th row in array_1
*/
// nothing left to do
if (k == n + 1) return;
/*
calculate next (k+1)-th row in array_2
*/
// swap arrays and repeat
recurse(k + 1, n, array_2, array_1);
}
int main()
{
int n, *array_1, *array_2;
if(scanf("%d", &n) != 1) return 1; // input error
if (n < 0) return 1; // invalid input
array_1 = (int*)calloc(n + 1, sizeof(int));
array_2 = (int*)calloc(n + 1, sizeof(int));
array_1[0] = 1;
recurse(1, n, array_1, array_2);
free(array_1);
free(array_2);
return 0; // done
}
Thanks everyone for answer :). This is my code:
#include <stdio.h>
#include <stdlib.h>
void recurse (int k, int n, int *array_1, int *array_2)
{
int i;
if(k==n+1) return;
for(i=1; i<=k+1; i++) array_2[i] = array_1[i] + array_1[i-1];
recurse(k+1, n, array_2, array_1);
}
void output(int n, int *array_1, int *array_2)
{
int i;
if(n%2!=0)
for(i=0; i<=n; i++) printf("%d ", array_1[i]);
else
for(i=0; i<=n; i++) printf("%d ", array_2[i]);
}
int main()
{
int n;
int * array_1;
int * array_2;
scanf("%d",&n);
if(n=='0')
{
printf("%d", 1);
return 0;
}
else if(n=='1')
{
printf("%d %d", 1, 1);
return 0;
}
array_1 = (int*)calloc(n+1,sizeof(int));
array_2 = (int*)calloc(n+1,sizeof(int));
array_1[0] = array_1[1] = array_2[0] = 1;
recurse(1, n, array_1, array_2);
output(n, array_1, array_2);
free(array_1);
free(array_2);
return 0;
}
C program for bubble sort using a minimum of 4 functions.(input,output,compute,main)
No global variables allowed.
No printf or scanf in compute.
No printf or scanf in main
Input should not call compute.
compute should not call output.
I haven't really understood pointers and functions.
#include <stdio.h>
void input(int* size, int* arr[])
{
printf("Enter the size of the array: ");
scanf("%d",size);
printf("Enter the elements of the array\n");
for(int i = 0;i < *size; i++)
{
scanf("%d", arr[i]);
}
}
void swap(int *x,int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void bubble_sort(int arr[100],int size)
{
for(int i = 0;i < size - 1;i++)
{
for(int j = 0;j < size - 1 - i;j++)
{
if(arr[j] > arr[j+1])
{
swap(&arr[j],&arr[j+1]);
}
}
}
}
void output(int size,int* arr)
{
printf("Sorted array\n");
for(int i = 0;i < size;i++)
{
printf("%d",arr[i]);
}
}
int main()
{
int* input_values[50];
int size;
input(&size, input_values);
bubble_sort(size,*input_values);
output(size, *input_values);
return 0;
}
No errors but showing segmentation fault.How do I solve this?
So your problem is here :
scanf(" %d", arr[i]);
You have to change this to :
scanf(" %d", &arr[i]);
This is the main problem, but there are a lot of others.
Also you have to change the order of parameters in
bubble_sort(size,*input_values);
to
bubble_sort(input_values,size);
and
output(size, *input_values);
to
output(size, input_values);
Also in order this to work at all i have changed the
scanf("%d", &arr[i]);
to
scanf(" %d", &arr[i]);
Actually your code is full of mistakes like the usage of scanf and the usage of pointers and arrays, the following is a workable version of you code see and compare:
#include <stdio.h>
void input(int* size, int arr[])
{
char chr;
printf("Enter the size of the array: ");
scanf( "%d%c", size, &chr );
printf("Enter the elements of the array\n");
for(int i = 0;i < *size; i++)
{
scanf("%d%c", &arr[i], &chr);
}
}
void swap(int *x,int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
void bubble_sort(int* size,int arr[])
{
for(int i = 0;i < *size - 1;i++)
{
for(int j = 0;j < *size - 1 - i;j++)
{
if(arr[j] > arr[j+1])
{
swap(&arr[j],&arr[j+1]);
}
}
}
}
void output(int* size,int arr[])
{
printf("Sorted array\n");
for(int i = 0;i < *size;i++)
{
printf("%d",arr[i]);
}
}
int main()
{
int input_values[50];
int s = 0;
int* size = &s;
input(size, input_values);
bubble_sort(size,input_values);
output(size, input_values);
return 0;
}
This question already has answers here:
Manipulate multidimensional array in a function
(4 answers)
Closed 8 years ago.
Could anyone explain why this doesn't print correctly?
This is basic program with functions to read and print an array. All seems to be according to what I read...
I'm new and can't seem to make pointers work.
Thanks in advance!
#include <stdio.h>
#include <stdlib.h>
#define SIZE 2
void readArray(int *a);
void printArray(int *a);
int main (int argc, char *argv[])
{
int array[SIZE][SIZE];
readArray(&array[SIZE][SIZE]);
printf("Array [1][2] = %d.\n\n\n", array[1][2]);
printArray(&array[SIZE][SIZE]);
system ("PAUSE");
return 0;
}
void readArray(int *a)
{
int i, j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
printf("Array [%d] [%d]: ", i, j + 1);
scanf("%d \n",&a);
}
}
}
void printArray(int *a)
{
int i, j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
printf("Array [%d] [%d]: ", i, j + 1);
printf("%d \n",*a);
}
}
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,a[2][2];
printf("Enter array\n");
for(i=0;i<2;i++) //take condition i<2 because of your default size
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Printing array\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d\t",*(*(a+i)+j));
}
printf("\n");
}
getch();
}
try this code
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I wrote a shell sorting algorithm which works perfectly on integer values, but gives me segmentation fault when trying to sort float numbers. Could you help me with this ? Thank you.
We have a homework at the university and my teacher wrote this program with insert sort and it worked. The homework for us was to rewrite it to shell sort (which I think I did correctly) and maybe expand it further more to sort strings and numbers from files.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
int int_cmp(const void *p1, const void *p2){
return *(int*)p1 - *(int*)p2;
}
int float_cmp(const void *p1, const void *p2){
if (*(float*)p1 == *(float*)p2){
return 0;
}
else if (*(float*)p1 < *(float*)p2){
return -1;
}
else {
return 1;
}
}
void shell_sort(void *v, int nr, int size, int(*p_cmp)(const void*, const void*)){
int i, j;
void *pv, *pi, *pj;
pv = malloc(size); //this is where I get segmentation fault
for (int gap = nr/2; gap > 0; gap/=2){
for (i = gap; i < nr; ++i){
for (j = i-gap; j >= 0; j-=gap){
pi = v;
pi = (char*)pi+j*size;
pj = (char*)pi+gap*size;
if (p_cmp(pi, pj) > 0){
memcpy(pv, pi, size);
memcpy(pi, pj, size);
memcpy(pj, pv, size);
}
else {
break;
}
}
}
free(pv);
}
}
int main(){
int a[20], n = 20;
float b[25], m = 25;
srand(time(NULL));
printf("Original integer array: ");
for (int i = 0; i < 20; ++i){
a[i] = rand() % 100;
printf("%i ", a[i]);
}
printf("Original float array: ");
for (int j = 0; j < 25; ++j){
b[j] = (float)rand()/(float)(RAND_MAX)*50;
printf("%f ", b[j]);
}
shell_sort(a, n, sizeof(int), int_cmp);
shell_sort(b, m, sizeof(float), float_cmp);
printf("\nInteger array after sort: ");
for (int i = 0; i < 20; ++i){
printf("%i ", a[i]);
}
printf("\nFloat array after sort: ");
for (int j = 0; j < 25; ++j){
printf("%f ", b[j]);
}
return 0;
}
free(pv); move to outside for-loop(after loop).