Why array doesn't print correct numbers? [closed] - c

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.

Related

Using if statement in passed 1-d array C, Size of the type 'int(int *)' is unknown or zero in function array(int *) [closed]

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 5 years ago.
Improve this question
#define SIZE 5
int array(int[]);
int main()
{
int my_array[SIZE];
int i,highest;
for(i=0;i<SIZE;i++)
{
printf("enter element %d of the array",i);
scanf("%d",&my_array[i]);
}
highest=array(my_array);
}//end main
seems like i passed the array correctly but when i t reaches my if statement i get multiple errors declaring
Size of the type 'int(int *)' is unknown or zero in function array(int *)
int array(int my_array[])
{
int i,largest;
largest=my_array[0];
for(i=0;i<SIZE;i++)
{
if(array[i]>largest)
{
largest=array[i];
}//end if
}//end for
return largest;
}//end array
Looks like you just made a typo.
Replace
if(array[i]>largest)
{
largest=array[i];
}//end if
by
if(my_array[i]>largest)
{
largest=my_array[i];
}//end if
and it should work !
You have some naming problems, that's all. Change your array name array to my_array in your function, because names of your function and array are the same.:
#include <stdio.h>
#define SIZE 5
int array_func(int[]);
int main()
{
int my_array[SIZE];
int i,highest;
for(i=0; i<SIZE; i++) {
printf("enter element %d of the array: ",i);
scanf("%d",&my_array[i]);
}
highest=array_func(my_array);
printf("%d\n", highest);
return 0;
}//end main
int array_func(int my_array[])
{
int i,largest;
largest=my_array[0];
for(i=0; i<SIZE; i++) {
if(my_array[i]>largest) {
largest=my_array[i];
}//end if
}//end for
return largest;
}//end array
In your function you write "array" instead of "my_array" which is the argument you have set,as seen in the parenthesis when you define your function.You can give any name to your argument however you must not use another name for it inside the function.

Stone codechef: What is wrong with the code? [closed]

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 7 years ago.
Improve this question
I am new to coding and finding my way!! I am solving a problem at codechef http://www.codechef.com/problems/RRSTONE.
All tests cases are passed at my side but the answer is still wrong. Any Help???
//codechef stones
#include<stdio.h>
int main(int argc, char *argv[])
{
unsigned int size; //N=size
unsigned long int k; //k = K
scanf("%u %lu",&size,&k);
long int a[size], max;
int i,j;
for(i=0;i<size;i++)
scanf("%lu",&a[i]);
i=j=0;
if(k==0)
goto printing;
if(k%2==1)
k=1;
else
k=2;
for(j=0;j<k;j++)
{
max=a[0];
for(i=0;i<size-1;i++)
{
if(max<a[i])
max=a[i];
}
for(i=0;i<size;i++)
a[i]=max-a[i];
}
printing: //removed this comment
for(i=0;i<size-1;i++)
printf("%d ",a[i]);
printf("%d",a[i]);
return 0;
}
I suggest you should look at the criteria properly.
Constraints
1 <= N <= 105
0 <= K <= 109
Ai does not exceed 2 * 10^9 by it's absolute value.
Here is modified code try with this.
And one more thing: Since you are learning understand the code first.
#include<stdio.h>
int main(int argc, char *argv[])
{
unsigned int size; //N=size
unsigned long int k; //k = K
scanf("%u %lu",&size,&k);
long long int a[size], max; //CHANGE HERE!!!! and so forth for long long
int i,j;
for(i=0;i<size;i++)
scanf("%lld",&a[i]);
i=j=0;
if(k!=0)
{
if(k%2==1)
k=1;
else
k=2;
for(j=0;j<k;j++)
{
max=a[0];
for(i=1;i<size;i++)
{
if(max<a[i])
max=a[i];
}
for(i=0;i<size;i++)
a[i]=max-a[i];
}
}
for(i=0;i<size;i++)
printf("%lld ",a[i]);
return 0;
}
EDIT: Sorry I forgot to put this note. There is issue sometimes with goto when you are using online compilers, so better to try with a clean approach and make sure you are selecting the correct option for source language (C-gcc or C99 whatever you're using) . Earlier I have experienced the same.

Printing garbage value instead of maximum value from an array in c [closed]

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 8 years ago.
Improve this question
I'm a newbie in programming.
I've tried this code to find out the maximum value in an array. I've also drawn the logic of this code on paper & it's coming out valid.
I'm wondering why this code is printing garbage value instead of max value.
Here is the code:
void main(){
int size, i, max=0;
printf("Enter the size of the array:\n");
scanf("%d", &size);
int a[size];
printf("Enter the elements:\n");
for(i=0; i<size; i++){
scanf("%d", &a[i]);
}
for(i=0; i<size; i++){
if(a[i]<a[i+1]){
max=a[i+1];
}
}
printf("\nMax value is %d", max);
}
You can try this code..
int main(){
int size, i, max=0;
printf("Enter the size of the array:\n");
scanf("%d", &size);
int a[size];
printf("Enter the elements:\n");
for(i=0; i<size; i++){
scanf("%d",&a[i]);
}
// Search Max Value
max = a[0];
for (i = 0; i < size; i++)
{
if (a[i] > max)
{
max = a[i];
}
}
printf("\nMax value is %d", max);
return 0;
}
You need to initialize max somewhere prior to searching the array (which the current copy of the post does do ... but the first one did not). However, if negative values are entered, it probably makes sense to use INT_MIN.
max = INT_MIN;
And then you need to compare the current max value against the array elements (rather than the array elements against themselves.
At this point, in your code, you're going outside of your array. You're also not comparing the element of the array to the current max, but to the next element in the array. This would be for sorting an array, but not finding the maximum value.
for(i=1; i<=size; i++){ /* i<=size */ you go one beyond
if(a[i]<a[i+1]){ /* when i==size, you're 2 outside of your array */
max=a[i+1];
}
}
You can have i<(size-2), but it's not very pretty.
You can also set max to the first element of the array, and loop from the second element.
max = a[0];
for(i=1 ; i<size ; i++){ /* loop to the end of the array */
if(a[i] > max){ /* compare current space to max */
max = a[i];
}
}
main has return type int, always. Valid prototypes:
int main()
int main(int argc, char* argv[])
Starting with 0 as initial value for maxinstead of INT_MIN means you have an implicit element of value 0.
Always test the return value of scanf, it might not succeed.
Your second loop tries reading a[size] and a[size+1], while your array only goes to index size-1.
Correct the termination condition to <.
Correct the comparison to compare a[i] with max.

array that accepts a number only once(code doesn't work) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to make an array that accepts each number just once and if the user tries to insert a number more than once,then he must enter an other number...can anyone help me please?
I have tried this so far:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int a[5];
int i,j,num;
scanf("%d",&num);
a[0]=num;
for(i=1;i<5;++i){
again: scanf("%d",&num);
if(a[i]!=a[i-1])
a[i]=num;
else
goto again;
}
for(i=0;i<5;i++)
printf("%4d\n",a[i]);
system("pause");
return 0;
}
but the code just doesn't work and i don't know why
it is observed from your code that given array must be filled but it should not contain redundant values.
following code iterates until the array is filled with no redundant value, once the array is filled it terminates.
int a[5],i=1,k=0,p;
int num;
scanf("%d",&num);
a[0]=num;
while(i<5)
{
scanf("%d",&num);
for(p=0;p<=k;p++)
{
if(a[p]==num)
{
break;
}
if(p==(k))
{
a[i]=num;
k=i;
i++;
}
}
}
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
hope this could help you
You are just comparing the new entered value with the previous one in a[i]!=a[i-1].
You better create a function to test the new value with the entire array, like
int valueExists(int num, int a[], int len) {
int i;
for (i = 0; i < len; i++) {
if (a[i] == num) {
return 1;
}
}
return 0;
}
Make sure you adding the new value to the array only after testing the value is not there.
again: scanf("%d",&num);
if (valueExists(num, a, i)) {
goto again;
} else {
a[i] = num;
}
(The loop you created with the goto can be replaced by a do-while loop, but that is not the issue here)

Define a function to get the elements of 2D matrix from user in C [closed]

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

Resources