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
#include <stdio.h>
int main() {
int i = 1;
int j = -1;
while(i)
i++;
while(-j)
j--;
i = i-1;
j = j+1;
printf("%d %d\n",i,j);
}
I want to know the range of int! this code can not get the right answer! But it can!
#include <stdio.h>
int main() {
int i = 1;
int j = -1;
while(i > 0)
i++;
while(j < 0)
j--;
i = i-1;
j = j+1;
printf("%d %d\n",i,j);
}
I don't know what are the differences between them!
Both of these code samples rely on undefined behavior to determine the maximum value of an int. Overflow is not defined for a signed number like int.
To find the maximum value of an int, you simply read the value INT_MAX.
If you truly want to calculate the value of INT_MAX in a portable way, then you should look into this answer: Is there a portable way to define INT_MAX?
Try limits.h from the standard C library. It contains the two constant definitions INT_MIN and INT_MAX, telling you the minimum and maximum values of an int respectively.
#include <limits.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
printf("int min: %d, int max: %d\n", INT_MIN, INT_MAX);
return 0;
}
You want to know the range of int?
#include <stdio.h>
#include <limits.h>
int main()
{
printf("%d, %d\n", INT_MIN, INT_MAX);
return 0;
}
Program output
-2147483648, 2147483647
And following the answer from #iharob, here is a similar solution that reaches the limit without UB.
#include <stdio.h>
int main()
{
int integer = -1, max = 0;
while (integer <<= 1)
max = (max << 1) | 1;
printf("%d\n", max);
return 0;
}
Lastly, a niftier solution (no loops) would be
printf("Max int %d\n", (unsigned)-1 >> 1);
If you actually want to compute the value, you can use this
int
main(int arcg, char **argv)
{
int integer;
int i;
integer = 0;
for (i = 0 ; i < 8 * sizeof (integer) - 1 ; i++)
{
integer |= (1 << i);
}
return 0;
}
and then the maximum will be integer and the minimum will be -integer
Related
I have tried different inputs and when it exceeds a 7 or 8 digit value it just shows some wrong answers as outputs but it worked fine with most of my cases.
#include <stdio.h>
#include <stdlib.h>
int bin(unsigned long long int n){//gave function for binary convertion
if(n==0)
return 0;
else
return (n%2+10*bin(n/2));
}
int main()
{
unsigned long long int n,x;/*I even gave high digit data type*/
int i, v, count=0, max=0;
scanf("%llu",&n); /*if input is >8-digit output is wrong*/
x = bin(n);
v = floor(log10(x))+1; /*Its length*/
int a[v];
for(i = v-1; i >= 0; i--){ /*string it in array*/
a[i] = x%10;
x = x/10;
}
for(i = 0; i < v; i++){
if(a[i] == 0){
count = 0;}
else{
count++;}
if(max < count){
max = count;}
}
printf("%d",max);/*I gave 99999999 output is 8 but its shows 9*/
}
Your program has a number of problems. Here is one example:
int bin(unsigned long long int n){
^^^
The function returns an int so the calculation will overflow for even small numbers:
printf("%d\n", bin(1023)); // will print 1111111111 (fine)
printf("%d\n", bin(1024)); // will/may print 1410065408 (ups - very bad)
Even if you change to
unsigned long long int bin(unsigned long long int n){
overflow will happen soon.
In I'll recommend that you look directly into the binary pattern of the number using the & operator.
I'll not solve the complete task for you but here is some code that may help you.
#include <stdio.h>
#include <stdlib.h>
int main()
{
size_t t = 1;
size_t limit;
size_t n;
if (scanf("%zu", &n) != 1)
{
printf("Illegal input\n");
exit(1);
}
limit = 8 * sizeof n; // Assume 8 bit chars
for (size_t i = 0; i < limit; ++i)
{
if (n & t)
{
printf("Bit %zu is 1\n", i);
}
else
{
printf("Bit %zu is 0\n", i);
}
t = t << 1;
}
return 0;
}
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 4 years ago.
Improve this question
I just started learning C. I am writing my code as follows:
#include <stdio.h>
#include <conio.h>
void main()
{
int i, s = 0;
clrscr();
for(i = 1 ; i <= 5 ; i++)
{
if ((i % 3 == 0) || (i % 5 == 0))
{
printf("%d\n", &i);
s = s + i;
}
}
printf("sum is: %d\n", &s);
getch();
}
But I am getting trouble in output, which is this:
The address operator & is unnecessary:
printf("%d\n", i);
printf("sum is: %d\n", s);
Correct your code from:
printf("%d\n", &i);
to:
printf("%d\n", i);
You do not need to use the & address operator.
printf("%d\n",&i); It should be printf("%d\n",i); and &s should be s Try below code...
#include<stdio.h>
#include<conio.h>
void main()
{
int i,s=0;
clrscr();
for(i=1;i<=5;i++){
if((i%3==0)||(i%5==0)){
printf("%d\n",i);
s=s+i;
}
}
printf("sum is:%d\n",s);
getch();
}
You can read how to use printf here.
In short printf does not require address of the variable but value of it - which is
opposite to scanf.
Remove the & operator to use printf like:
printf("%d\n",i);
and
printf("sum is:%d\n",s);
See:
#include<stdio.h>
#include<conio.h>
int main(void)
{
int i,s=0;
clrscr();
for(i=1; i<=5; i++)
{
if((i%3==0)||(i%5==0))
{
printf("%d\n",i);
s=s+i;
}
}
printf("sum is:%d\n",s);
getch();
return 0;
}
Output:
3
5
sum is:8
Other improvement would be to comply with C standard and declare main as int main(void) instead of void main() and return a value from the program.
#include <stdio.h>
main() {
int i;
printf("the multiples of 3 or 5 are :");
for (i = 0; i <= 10; i++) {
if ((i % 3 = 0) || (i % 5 = 0)) print("%d\n", i);
else continue;
}
}
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 5 years ago.
Improve this question
#include <stdio.h>
#include <stdlib.h>
int factorial(int n) {
int f;
for(f = 1; n > 0; n--)
f *= n;
return f;
}
int ncr(int n,int r) {
return factorial(n) / ( factorial(n-r) * factorial(r) );
}
int main(int argc, char* argv[]) {
int n, i, j;
n = atoi(argv[1]);
for(i = 0; i <= n; i++) {
printf("1");
for(j = 1; j <= i; j++)
if(i == j && j > 0) printf(" 1");
else printf(" %d", ncr(i, j));
printf("\n");
}
return 0;
}
If I test it with the argument of a number that is above 12, I get strange numbers from row 12(?).. Why is this happening? should I use malloc or array? Can someone please change this codes into an array or malloc? Thanks.
Why strange? Factorial 13 is 6227020800, which exceeds INTMAX so you wrap into negative land. "long long" would likely work. Get to know the MAX and MIN values in for size limits.
Always be sure of your ranges before you start designing a program.
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 7 years ago.
Improve this question
Problem Link: https://www.codechef.com/problems/PERMUT2
Problem : Getting non ambiguous for all test cases. There is absolutely no problem in executing the program, no errors.
Can you please point out the mistake in my code/algorithm:
#include <stdio.h>
#include <stdlib.h>
int index_func(int number, int *array, int x);
int main(){
int n;
scanf("%d", &n);
int *nums = (int*)malloc(n*sizeof(int));
int i;
for(i=0; i<n; i++){
scanf("%d", &nums[i]);
}
int j;
int counter = 0;
for(j=0; j<n; j++){
if(nums[j] != index_func(j+1, nums, n)){
counter = 1;
break;
}
}
if(counter == 0){
printf("ambiguous\n");
}else{
printf("non ambiguous\n");
}
return 0;
}
int index_func(int number, int *array, int x){
int z, index;
for(z=0; z<x; z++){
if(number == array[z]){
index = z;
return z;
}
}
}
The numbers in the array start with one, but the indices in C arrays start with 0. A quick fix to your program would be to add one to the returned index when you compare it to the current number:
if (nums[j] != index_func(j + 1, nums, n) + 1) ...
An alternative solution is to adjust the array data by subtracting one after you scan it, so that the array contains zero-based numbers.
A problem may arise with larger arrays, because every call to index_func scans the whole array from the beginning and will traverse half of it on average. The solution will be correct, but very slow.
But you don't have to determine the index to do the comparison. It is sufficient to check whether the number at the index of the current number is the current index. That leads to this function:
int is_ambiguous(const int *array, int n)
{
int i;
for (i = 0; i < n; i++) {
if (array[array[i] - 1] != i + 1) return 0;
}
return 1;
}
Some notes on your original code:
You should return an invalid index, probably −1, from index_funct when the nuber isn't in the array. I know, this shouldn't happen here, but next time you copy and paste the code and the missing return value might bite you.
You don't really need the variable index in index_funct. Separating pieces of code into small functions can make the program control easier. Compare the above function is_ambiguous with your inline solution with a counter variable and a break.
When you allocate, you must also free, which you don't.
try this solution:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int index_func(int number, int *array, int x);
int main(){
int n;
scanf("%d", &n);
int *nums = (int*)malloc(n*sizeof(int));
int i;
for(i=0; i<n; i++) {
scanf("%d", &nums[i]);
}
int j;
int counter = 0;
for(j=0; j<n-1; j++){
if((abs(nums[j+1] - nums[j]) != abs(n-1)) && (abs((nums[j+1] - nums[j]) != 1)))
{
counter = 0;
}
else
{
counter = 1;
}
}
if(counter == 0){
printf("ambiguous\n");
}else{
printf("non ambiguous\n");
}
free(nums);
return 0;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I've searched around, but I haven't found anything about my question.
I want create an algorithm in C that finds the smallest number in an array that can hold 10 values.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void) {
int array[10];
int i, smaller;
srand(time(NULL));
for (i = 0; i < 10; i++) {
array[i] = rand() % 100 + 1;
}
// Find smaller number in the array
for (i = 0; i < 10; i++) {
...
}
printf("Smaller: %d\n", smaller);
return 0;
}
Any tips on how can I do?
Assign first element of array to smaller
smaller = array[0];
Start a loop with i = 1 and compare elements with smaller. If smaller is greater than any of array element then replace it with that elment otherwise left it as it.
Since your array is such a small size and unsorted, you can do a simple O(n) linear search like this:
int main(void)
{
int array[10];
srand(time(NULL));
int i;
for (i = 0; i < 10; i++)array[i] = rand() % 100 + 1;
int smallestSoFar=array[0];
for (i = 1; i < 10; i++) if(smallestSoFar>array[i]) smallestSoFar=array[i];
printf("Smallest value: %d\n", smallestSoFar);
return 0;
}
What is happening is you assume that the first element in the array is indeed the smallest. Then you iterate through the entire array one by one, and change your mind if you see a smaller value;
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*
follow good practice and never hardcode an array
use symbolic names instead that way if you have to
increase or decrease the size of the array you only
have to change the value here
*/
#define NO_OF_ELEMENTS 10
int main(void)
{
// declare and initialize all elements to 0
int array[NO_OF_ELEMENTS] = {0};
int smallest = 0, largest = 0, i = 0;
srand(time(NULL));
for(i = 0; i < NO_OF_ELEMENTS; i++)
{
array[i] = (rand() % 100) + 1;
// Compare against each element as you go to find the largest
if(largest < array[i])
{
largest = array[i];
}
printf("\nElement %d: %d", i, array[i]);
}
// assume smallest element is in the first position
smallest = array[0];
for(i = 0; i < NO_OF_ELEMENTS; i++)
{
if(smallest > array[i])
{
smallest = array[i];
}
}
printf("\n\nSmallest element in array is: %d", smallest);
printf("\nLargest element in array is: %d", largest);
getchar();
return 0;
}
That program should help you out it will return the largest and smallest value in the array.