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 6 years ago.
Improve this question
I try to find largest number using array and pointer, but program gives errors on "finding step". Can you tell me where is my mistake?
void find_two_largest(int a[], int n, int *largest)
{
int i;
for(i=0;i<n;i++)
{
printf("enter %d. value: ",i+1);
scanf("%d",&a[i]);
}
int max=a[0];
for(i=0;i<n;i++)
if(a[i]>max) max=a[i];
largest=&max;
printf("%d",*largest);
}
int main()
{
int n,i,a[100],*lar=NULL;
printf("how many elements you want to store?\t");
scanf("%d",&n);
find_two_largest(a, n, lar);
return 0;
}
The main program is not initializing *lar, the find_two_largest function is initializing max before set user values, so max could be any value in memory.
void find_two_largest(int a[], int n, int *largest)
{
int i,max;
for(i=0;i<n;i++)
{
printf("enter %d. value: ",i+1);
scanf("%d",&a[i]);
}
//initialize max after entering values
max=a[0];
for(i=0;i<n;i++)
if(a[i]>max) max=a[i];
*largest=max;
printf("%d",*largest);
}
int main()
{
int n,i,a[100],*lar,*slar;
//initialize;
*lar=INT_MIN;
printf("how many elements you want to store?\t");
scanf("%d",&n);
find_two_largest(a, n, lar);
return 0;
}
Remove * from lar definition.
like this it should be :
int n,i,a[100], lar,*slar;
and then add an & to function call :
find_two_largest(a, n, &lar);
and after that put max = a[0] after a[0] value set.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
So the problem is:" Create a program, that inputs the elements(integers) of a matrix a[n][m] and finds the element with the smallest value amongst the the elements with the biggest value of every column."
So the way I understand it, I have to go through every column and find the biggest number in each, so I will end up with 4 numbers. From these 4 numbers I have to print out the smallest.
int main()
{
int n,m;
int A[n][m];
int B[m];
int max;
int min;
int i,j;
printf("Enter a number for n: ");
scanf("%d",&n);
printf("Enter a number for m: ");
scanf("%d", &m);
for(i=1; i<=n;i++)
{
for(j=1; j<=m;j++)
{
printf("Enter a number [%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
}
}
for(j=1; j<=m; j++)
{
max=A[j][1];
for( i=1; i<=n; i++)
{
if(max<A[j][i])
{
max=A[j][i];
B[j]=max;
}
}
}
min=B[1];
for(i=1; i<=m; i++)
{
if(min>B[i])
{
min=B[i];
printf("%d ", B[i]);
}
}
printf("Min is: %d\n", min);
return 0;
}
My question is where would the error be? It must be a logical one.
I apologize for not asking my question as clearly as most of you in this forum, but it's my first time asking here. Please if you answer, explain it like you would to a beginner.
Thanks!
C programs are generally executed from top to bottom. Therefore you cannot do this:
int n,m;
int A[n][m];
Because n and m has not been given any values. To give them values further down doesn't mean that the line int A[n][m]; will somehow get re-executed.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have the following code that works fine:
#include <stdio.h>
#include <stdlib.h>/*need this for rand()*/
#include "random.h"
#include <time.h>/*for time() function*/
int main()
{
int arr[5], a = 0;
printf("enter 5 array elements\n");
scanf("%d", &arr[0]);
scanf("%d", &arr[1]);
scanf("%d", &arr[2]);
scanf("%d", &arr[3]);
scanf("%d", &arr[4]); /*scan all elements*/
srand(time(NULL)); /*set the seed*/
a = arr[rand() % ARR_SIZE(arr)];/*from .h file*/
printf("%d\n",a);/*print the random element generated above*/
return 0;
}
It picks a random integer for an array of 5 integers.
I need the following modifications to it:
A function should accept two parameters — an array of void pointers and the array length. It should return a void pointer.
The function should pick an element from the array at random and return it.
int main() must seed the random number generator and then call the function. Then finally it should print the random element that was generated.
I don't know how to modify it to meet above requirements.
Here are the random.h file contents:
#define ARR_SIZE(arr) ( sizeof((arr)) / sizeof((arr[0])) )
This should do the trick.
void* getRandomElement(void** array, int size)
{
int* arr = (int*)*array;
return (void*)&(arr[rand() % size]);
}
int main(void)
{
int arr[5];
void* p = (void*)arr;
printf("enter 5 array elements\n");
scanf("%d", &arr[0]);
scanf("%d", &arr[1]);
scanf("%d", &arr[2]);
scanf("%d", &arr[3]);
scanf("%d", &arr[4]); /*scan all elements*/
srand(time(NULL)); /*set the seed*/
printf("random element %d\n", *(int*)getRandomElement(&p, ARR_SIZE(arr)));
return 0;
}
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 4 years ago.
Improve this question
Following code:
#include <stdio.h>
int izbaciSveProste(int n, int x[], int y[])
{
int i;
int flag=0;
for(i=2; i<n/2; i++)
{
if(n%i ==0)
{
flag =1;
break;
}
}
if(flag==1)
return 0;
else
return 1;
}
int main()
{
int i,j,n,x[100],y[100];
printf("Koliko elemenata zelite u polju?\n");
scanf("%d", &n);
printf("Enter elements in array:- ");
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
int len = sizeof(x)/sizeof(x[0]);
for(i=0; i<len; i++)
{
if(izbaciSveProste(x[i]))
{
for(j=i; j<len; j++)
{
x[j] = x[j+1];
}
i--;
len--;
}
}
printf("Elementi nakon brisanja su:\n");
for(i=0; i<len; i++)
printf("%d\n",y[i]);
printf("\n");
return 0;
}
Purpose of this program should be to delete all prime numbers from array x[] with n elements, remaining elements should be rewritten in array y[] and show count of elements in array y[] in the end.I believe that function is okay and error is in main() specifically in storing y[].
Your function int izbaciSveProste(int n, int x[], int y[]) requires three arguments. Your code izbaciSveProste(x[i] passes one argument. That's not much enough. The compiler tells you that fact with the error message:
error: too few arguments to function 'izbaciSveProste'
Your function prototype has 3 parameters:
int izbaciSveProste(int n, int x[], int y[])
When you call the function, you only provide 1:
if(izbaciSveProste(x[i]))
The compiler wants to get all 3.
As your function doesn't even touch the 2 array parameters, you might simply remove them from the function definition and only take 1 integer.
Another problem:
You print y[i] in your loop but you never assign any value to that array.
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 8 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
#define N 10
float func ( int *arr, int n, int *count )
{
int *p,i;
float sum=0;
p=arr;
for (i=0;i<n;i++)
{
sum += *p;
p++;
}
sum /= n;
count=0;
p=arr;
for(i=0;i<n;i++) //לולאה שעוברת על כל המערך ומוסיפה כל תוכן של איברבמערך שיותר גדול מהממוצע
{
*count+= (*p>sum);
p++;
}
return sum;
}
void main()
{
int i, count=0, arr[N]={0}, n=N;
for(i=0;i<N;i++)
{
printf("Please enter your grade\n");
scanf("%d",&arr[i]);
}
for(i=0;i<N;i++)
printf("%d ",arr[i]);
printf("The average in class is: %f and the number of students that had the best grades(MORE) are: %d \n",func(arr,n,count),count);
}
I need some help on this program.
the code is above,
the program need to get from the users 10 grades and then print average and count how many grades is more then the average
Your main function is incorrectly declared. You should compile with all warnings and debug info (e.g. gcc -Wall -g if using GCC, which is probably used by your Codeblocks IDE...) then you should use the debugger (e.g. gdb). And you should test the result of scanf(3)
BTW,
count = 0; // better written as count = NULL;
is wrong: it is clearing the pointer. You probably want
*count = 0;
Also, your last printf is supposing some order of evaluation (since you expect the call to func to change count before printing count), so is undefined behavior. You need:
float avg = func(arr,n,&count);
// from http://stackoverflow.com/a/25382154/841108
printf("The average in class is: %f and the number"
" of students that had the best grades are: %d \n",
avg,count);
Plese show or tell your teacher that you got help on SO (he will find out anyway)
BTW, I can't understand why students are asking their homework on the web. They don't learn anything by doing that, and their teacher will notice anyway.
You are calculating sum and count in func, so you can't return two values at a time. so count the marks which are greater then average in main().
Try this-
#include <stdio.h>
#include <stdlib.h>
#define N 10
float func ( int *arr, int n)
{
int *p,i;
float sum=0;
p=arr;
for (i=0;i<n;i++)
{
sum += *p;
p++;
}
sum /= n;
printf("%lf \n",sum);
return sum;
}
int countfun(int *arr, float sum)
{
int i,count = 0;
for(i=0;i<N;i++)
{
if(arr[i] > sum)
count++;
}
return count;
}
void main()
{
int i, count=0, arr[N]={0}, n=N;
float sum=0;
for(i=0;i<N;i++)
{
printf("Please enter your grade\n");
scanf("%d",&arr[i]);
}
for(i=0;i<N;i++)
printf("%d ",arr[i]);
sum = func(arr,n);
count = countfun(arr,sum);
printf("The average in class is: %f and the number of students that had the best grades(MORE) are: %d \n",sum,count);
}
You can use a getchar() just after the last printf() to wait for a character.
You think that the execution window closes without printing. That is wrong. It does print and then exits before you can even see the output.
P:S - You have some problems with your code as mentioned in the rest of the answers. Fix them and then try this
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I can't finish this code please help me! I have to matrix, and the program reads items of matrix in function
int main(int argc, char *argv[])
{
int r1,c1;
printf("Enter dimension of first matrix :");
scanf("%d %d",&r1,&c1);
int m1[r1][c1];
GetArray(m1,r1,c1);
system("PAUSE");
return 0;
}
void GetArray(int arr[][],int _row,int _column)
{
int i,j,num;
printf("Enter number: ");
for(i==0;i<_row;i++){
for(j==0;j< _column;j++){
scanf("%d",&num);
arr[i][j]=num;}} //give error in this line
}
In C programming, == is used for comparison and = is used for assignment operations. You would definitely want to assign values to j and i in your for loops. In your case, you are not initializing the loop variables (when you declare them in the beginning of the function) and since they get garbage values when they are not initialized, you try to reach beyond the bounds of the array you are using in the for loops, thus getting a segmentation fault.
int main(int argc, char *argv[])
{
int r1,c1;
printf("Enter dimension of first matrix :");
scanf("%d %d",&r1,&c1);
int **m1;
for(int i = 0; i<r1 ;++i) // use c99 !
m1[i] = malloc(c1* sizeof(int));
GetArray(m1,r1,c1);
system("PAUSE");
for(int i = 0; i<r1 ;++i)
free(m1[i]);
return 0;
}
void GetArray(int ** m1,int _row,int _column)
{
int i,j,num;
printf("Enter number: ");
for(i=0;i<_row;i++){
for(j=0;j< _column;j++){
scanf("%d",&num);
m1[i][j]=num;}} //give error in this line
}
Untested