C Find the largest number in array [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 2 years ago.
Improve this question
I'm new to C and what I'm trying to do is ask the user to input 10 numbers in which it will then find the largest one. However, sometimes it just prints the last number inputted as the largest and I'm not sure where I'm going wrong.
void find_largest()
{
int numbers[10];
for (int i = 0; i < 10; ++i)
{
printf("Enter a number: ");
scanf("%d", &numbers[i]);
}
int largest = numbers[0];
for (int i = 1; i < 10; ++i)
{
if (numbers[i] > numbers[0])
{
largest = numbers[i];
}
}
printf("%d\n", largest);
}

Basically, you assigned the value of largest element is array's first element. But after this operation you should check the largest element with all of the array's number. If the one of the element in the array is larger then the largest variable, you should assign this value to the largest variable until checking the last element.
So you should change numbers[i]>largest

Related

why n mod 10 in loop doesn't show output [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 1 year ago.
Improve this question
I have solve a basic problem in c that is count the digits in integer and I have written -
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int i;
while(n!=0)
{
n %= 10;
++i;
}
printf("%d",i);
}
I already know that above code is wrong, I should write n/=10; instead of n%=10; but I wants to know why it is not printing even value of i i.e 0.
If I have written any wrong so please ignore it ,I am new here..
If the number n is not divisible by 10 then the value of this expression (the remainder of the division)
n %= 10;
will never be equal to 0.
Also the variable i is not initialized.
int i;
You should write
int i = 0;
do
{
++i;
} while ( n /= 10 );
printf( "%d\n", i );

Sort negative elements from an array [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 4 years ago.
Improve this question
This is part of a code I am writing as an assigment in C.
What I am trying to do is to detect negative numbers in an array and fill another array with these negative numbers. Meanwhile, I'd like to count those negative numbers.
All of this sounds pretty easy and everything seems to work out until I try to print the number of negative elements. This is what I get:
6422204 numbers are negative.
I simply cannot figure out what is wrong, or even if this is the good way to do it.
Here's the code:
double arr[10] = {21, -3.4, 65, 7.6, -12, 66, 10, -2, 5, 135};
int length = sizeof(arr)/sizeof(double);
printf("There are %d numbers in the array.\n",length);
for(int i = 0; i < 10; i++)
{
printf("[%d]=%.1lf ", i, arr[i]);
}
int neg = 0;
double negative[10];
for(int j = 0; j < 10; j++)
{
if(arr[j] < 0)
{
negative[j] = arr[j];
neg++;
}
}
printf("\n\n%d numbers are negative.", &neg);
Thanks for the answer!
Remove & in printf:
printf("\n\n%d numbers are negative.", neg);

Fill a array with numbers from 0-100 [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
I'm trying to fill an array with integers 0-100.
Here's my code:
int main()
{
int a[100];
for(int i = 0; i < a; i++)
{
a[i] = i;
printf("\n%d\n", a[i]);
}
}
I get this error:
comparison between pointer and integer (int and int) over the for line.
I can't figure it out. Any thoughts?
The cancel condition in your for loop is wrong. i<a You are comparing an int i variable with the pointer a that points to the memory location of the array. You would need to calculate the length of the array:
for(int i=0; i<sizeof(a)/sizeof(int); i++) {
But you could have found this solution in this 9 year old answer
Yes you are comparing pointer with an integer. This is why the error.
for(size_t i=0; i<sizeof(a)/sizeof(a[0]); i++) {
is what you wanted it to be and can be done in C language. Remember that sizeof operator results in value in size_t.
If you are not aware what that sizeof is doing - it is basically total number of bytes that the array object has - divided by each element's size - resulting in number of elements.
In case you are thinking from where did that pointer come?
Note one thing here a is converted into pointer (array decaying)to the first element - so it is nothing other than a pointer to the first element.
This should work for you,
int main()
{
int a[100];
int n = sizeof(a) / sizeof(a[0]); //Get the size of the array
for(int i=0; i<n; i++) { // loop through each elements of the array
a[i] = i;
printf("\n%d\n", a[i]);
}
}

Program working fine for smaller values but throwing Segmentation Fault for bigger values [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 7 years ago.
Improve this question
In the following program when the value of N is less than 100 the program is executing perfectly but for bigger values of N its showing segmentation fault.I sit because of less memory or anything wrong with program??
#include<stdio.h>
int main()
{
int N,iteration,MAX_ITERATONS;
int i,j,k,n,index,boundary1,boundary2;
double h[2][100][100];
int current = 0;
int next = 1;
printf("Enter the number of points\n ");
scanf("%d", &N);
boundary1=0.4*N;
boundary2=(0.6*N);
printf("The boundary is between %d and %d .\n",boundary1,boundary2);
for(k=0;k<2;k++)
{
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if((i==0)&&(j>=boundary1&&j<boundary2))
h[k][i][j]=100;
else
h[k][i][j]=20;
}
}
}
printf("Initial Values\n");
index = N/10;
for(i=0;i<N;)
{
for(j=0;j<N;)
{
printf("%.2f\t",h[0][i][j]);
j=j+index;
}
i=i+index;
printf("\n");
}
}
When N > 100, h is accessed to an index greater than 100, inside the nested for loop
h[k][i][j]=100;
but h is defined as
double h[2][100][100];
You are going out of bounds for h
If you want N as greater than 100 you need to redefine h or malloc it.

Summing elements of a 3d 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 7 years ago.
Improve this question
Im trying to sum the elements of a 3d array in C. The code recognizes that position check[1][1][0]=4 and adds 4 to sum when the loop reaches this position. However for the rest of the array it continues to add on this value again, and then it adds the total sum again for the rest of the positions of the array. Can anyone see why?
#include <stdio.h>
main() {
int check[3][3][3]={ 0 };
int size=2;
int i,j,k,sum=0;
check[1][1][0]=12;
for(k=0;k<size;k++) {
for(j=0;j<size;j++) {
for(i=0;i<size;i++) {
printf("i=%d, j=%d,k=%d, checkijk=%d ",i,j,k,check[i][j][k]);
sum+=sum+check[i][j][k];
printf("sum=%d\n", sum);
}
}
}
printf("The sum is %d\n",sum);
}
sum+=sum+check[i][j][k];
should be
sum+=check[k][j][i];
And if you want to sum all the values int size = 2; must be int size = 3;

Resources