Greatest product of consecutive digits - c
I need to find the greatest product of 10 consecutive digits in the 150-digit number in C but i cant see whats wrong.
I used nr[] to store the 10 consecutive numbers and nto store the biggest 10 number multiplie.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int array[150]={7,3,1,6,7,1,7,6,5,3,1,3,3,0,6,2,4,9,1,9,2,2,5,1,1,9,6,7,4,4,2,6,5,7,4,7,4,2,3,5,5,3,4,9,1,9,4,9,3,4,9,6,9,8,3,5,2,0,3,1,2,7,7,4,5,0,6,3,2,6,2,3,9,5,7,8,3,1,8,0,1,6,9,8,4,8,0,1,8,6,9,4,7,8,8,5,1,8,4,3,8,5,8,6,1,5,6,0,7,8,9,1,1,2,9,4,9,4,9,5,4,5,9,5,0,1,7,3,7,9,5,8,3,3,1,9,5,2,8,5,3,2,0,8,8,0,5,5,1,1};
int i,l,j,nr[10];
long int n=1,k;
for(i=0;i<140;i++){
k=1;
for(j=i;j<i+10;j++){
k=k*array[j];
}
if(n<k){
for(l=0;l<10;l++){
nr[l]=array[i+l];
}
n=k;
}
for(i=0;i<=9;i++){
printf("%d ",nr[i]);
}
return 0;
}
}
tryfor(i=1;i<=150;i++);
because you need 150-digit number
The biggest problem you have is that you have a return inside your outer for loop. That means you'll be exitting from your loop after you've looked at the first batch of 10 numbers.
You also have a problem in that you're printing your nr array inside your outer loop, so it will print a lot of stuff. You probably want it outside, just before your return.
Fixing those gives a string of:
9 4 9 4 9 5 4 5 9 5
I haven't checked, but there's a lot of 9's in there and nothing smaller than 4 so can easily imagine it's the largest string once multiplied.
Related
I tried make a bubble sort with C but it's not working (It's doing bubble sort 10 times)
first i giving how many numbers will i enter then i enter numbers. he need make bubble sort to them but its writing like======> 1 3 2 6 = 0 0 0 0(but it must be like 1 2 3 6(small to big)) the app how i want= 7 1 5 2 7 4 7 3 1 2 3 4 5 7 7 #include<stdio.h> int main() { int numbers[500000]; int counter=0; int howmany; scanf("%d",&howmany);//getting how many numbers will we enter int howmany2=howmany;//we will use this for write all numbers after all while(howmany>0)//getting all numbers { scanf("%d",&numbers[counter]); howmany--; counter++; } int checker1,checker2;//its gonna check first and second number, then second and third...etc int hm=howmany-1;//its gonna check entered number-1 times(1,2,3)={1,2},{2,3} int clone1;//later we will copy numbers[checker1] int tentime=10;//we gonna do bubble sort 10 times while(tentime>0)//doing bubble sort { checker1=0; checker2=1; while(hm>0) { if(numbers[checker1]>numbers[checker2]) { clone1=numbers[checker1]; numbers[checker1]=numbers[checker2]; numbers[checker2]=clone1; } checker1++; checker2++; hm--; } tentime--; } int counter2=0; while(howmany2>0)//showing new number sort on screen { printf("%d ",numbers[counter]); howmany2--; counter2++; } printf("\n"); return 0; }
You have several problems in your code: At the end of your first while loop howmany will be 0. As a result hm will be set to -1 and the sort loop (while hm > 0 ) will never run. When you print out the results are using counter as the array index (this is 4 which is out of bounds and never changes since you are incrementing counter2. As a result you are printing out an undefined value (0 in your case) four times. Declaring an array of size 500000 may blow up your stack. Even if not it is way larger than you need
Array sorting using special XOR condition
I have a problem in understanding the condition of my program, I can hardly explain it in a sentence so I will explain the whole point of program. So for my homework I got to make a program that will ask user to enter number N which will represent the number of elements in array, then user enters elements of that array (assuming that user will enter correct number of elements) program then needs compare every number from that array with a number X with XOR (^) Operator. The task is to find a minimum value for that X in which will the resulting array have elements in ascending order. It sounds a bit complicated but this is how it should work: You enter a number N: For example lets use 4. Then you enter 1D array of 4 elements : Lets use 4 2 3 1 Then program needs to use a number X (do while loop) to test every number from this array with that number and if that number is >= to the previous one, it should continue to check the next and next until it reaches the number N If every element is sorted in ascending(equal counts as ascending) order it should display that number. So for our example 4 2 3 1 when you use XOR operation with every one of them with the X=6 you get array that looks like this 2 4 5 7 which is in ascending order. To explain: 4 in binary is 100 ; X in binary is 110 if you use XOR on those you get 010 which is 2, and do as follows for the rest ( program does everything) So I made the program,everything works great returning good values for every example that we have for reference, my only problem is that I don't know when to stop looking for that number X, or how should I know that minimum X for that array of numbers doesn't exist. In that case my program runs forever and don't return any value,so basically an infinity loop. I need to use code that is simple so nothing too complicated because this is a course "Introduction to programming" and they won't accept anything that was made using complex algorithms or something like that. EDIt: The program should display -1 if there are no X. Here is the code : #include <stdio.h> int main() { int matrix[100]; int n; int i; int index=1; int x=0; int start=0; int end=0; printf("Enter N: "); scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d",&matrix[i]); } index=1; x=0; do { start=matrix[index]^x; if((matrix[index+1]^x) >= start) index=index+1; else x++; if(index==n){ printf("X=%d",x); end=1; break; } } while(end!=1); return 0; }
Getting too much zero while reading binary files in
I'm doing a program that read the whole binary file (in my case, a picture), reading 2 by 2 bytes and printing how many times each byte (from 0000h to FFFFh) appear in the file, but I'm having an issue regarding the quantity of zeros I'm capturing. Don't know exactly if it's a common case, but I'm feeling there's something wrong by allocating the array. There's something like: bit occuurrences 0 62354 1 13 2 4 3 5 4 2 5 2 6 0 7 2 . . . . 65535 0 What do you guys think I'm doing wrong? Follow the code: #include <stdio.h> #include <stdlib.h> int main() { unsigned short int *n; int cont=0; long lsize,i,j; FILE *arq=fopen("C:\\Users\\NB\\Documents\\Testes Allegro\\Trabalho PAQ\\imagens\\426.png","rb"); FILE *out=fopen("saida.csv","w"); fseek(arq,0,SEEK_END); lsize=ftell(arq); rewind(arq); n=(unsigned short int*)malloc(sizeof(unsigned short int)*lsize); fread(n,sizeof(short int),lsize,arq); fprintf(out,"bit,quantidade\n"); for(i=0x0000;i<=0xffff;i++) { for(j=0;j<lsize;j++) { if(n[j]==i) cont++; } fprintf(out,"%li,%d\n",i,cont); printf("%li,%d-",i,cont); cont=0; } fclose(arq); fclose(out); free(n); return 0; }
You are allocating short ints but measuring & reading bytes. Your array n will not work in the way you want. You will need to size n to lsize/2 times (+ an edge case for odd sizes) and adjust your loop accordingly. Your looping is very inefficient too!
Displaying primes from 2-10,000 in C
I'm struggling with this (optional) problem my professor recommended I try. Basically, my task is to write a program which displays all prime integers from 2-10,000 using my own user-defined function to determine prime-ness. It sounded simple enough but I'm having major difficulties debugging my program. For some reason, my code only displays 2 and 3 before ending. #include<stdio.h> //function declaration int prime(int); //main body int main(void) { int x=2, y; for (x=2;x<=30;x++) { y=prime(x); if (y!=0) printf("%d\n", x); } getchar(); return(0); } //function definition int prime(int x) { int y; for (y=2; y<=(int)sqrt(x); ++y) { if (x%y==0) return 0; } if (y==(int)sqrt(x)) return 1; } Instead of returning 1 if x is prime, my prime checking function seems to return a random large number (2686xxx) but that shouldn't be an issue because all primes return 0. If I run something like: if (y==0) printf("%d\n", x); I see a list of all non prime numbers. If I run something like: printf("%d %d\n", x, y); I see a list of all integers from 2-10,000 and the result of my prime checking function (0 for non-primes, 2686xxx for primes). Why doesn't the opposite (y!=0) display a list of prime numbers? What is causing my code to stop after just displaying 2 and 3? Why is my prime function returning a weird integer instead of 1? Finally, I'm still a beginner but how can I write better code in general? I don't think I'm breaking any of the standard accepted practices but how can I make my code more clean or efficient? Thanks in advance for the help!
Your loop continues if y==(int)sqrt(x). So when it finishes, they're not equal. What you wanted is: if (y>=(int)sqrt(x)) return 1; But this is not needed at all. Just return 1; is sufficient. You've already returned zero if the number isn't prime. If you wanted only a single return statement: int prime(int x) { bool isPrime = true; int y; for (y=2; y<=(int)sqrt(x); ++y) { if (x%y==0) { isPrime = false; break; } } return isPrime; }
Don't use the sqrt() function. In mathematics if you have 'x = sqrt(y)'. If you square both sides you will get something like this 'x * x = y'. This expression in c is tremendously faster than the sqrt function. Thus instead of doing: y <= (int)sqrt(x) Have you for loop guard be something like this: y * y <= x Here is a running example of your problem: Primes 2 -> 10000
At the end of your prime function just return 1. If it wasn't prime it would have returned 0 earlier. Right? As it is you've made a function which sometimes returns nothing at all. Which means that it returns whatever random value happens to be in the register.
You can use sieve of eratosthenes or sieve of atkin to mark all the prime numbers in an array and then display the prime numbers. It will be time efficient although it incurs some space complexity. e.g if you want to display prime numbers from 1 to 10 Leave of 1. Its not a prime. Its neither prime nor composite. So start from 2. consider this array of size 10 = 2 3 4 5 6 7 8 9 10 Traverse from 2. If an element is not highlighted highlight all its multiples. i.e for 2 highlight its multiples 4 6 8 10 ==> 2 3 4 5 6 7 8 9 10 For 3 do the same ==> 2 3 4 5 6 7 8 9 10 Then do it for the rest of the no.s i.e. 5 and 10 (Here 7 dont have multiple) Finally print the non highlighted elements. 2,3,5,7. Repeat this procedure for any other ranges.
Since you are interested in writing computer programs for prime numbers, perhaps you would like to turn this paper into a computer program. It deals with prime numbers and is similar to the sieve you were trying to create in C. https://graviticpropulsion.files.wordpress.com/2015/04/prime-number-theory.pdf I'm not sure if it is faster or less memory intensive, but I'm curious myself how high of a prime number it can find before it becomes too intensive for a computer.
Binomial coefficient in C
Here you can find the problem I'm trying to solve: For integers n and k (0<=k<=n<1001) determine (binomial coefficient). Input The first line of the standard input contains one integer t (t<1001) which is the number of test cases. In each of the next t lines there are numbers n and k. Output For each test print (binomial coefficient). Example: Input 3 0 0 7 3 1000 2 Output: 1 35 499500 I can't seem to find anything wrong in my solution (other than it's written very poorly - I've started programming quite recently): #include <stdio.h> int main() { unsigned long int t,n,k,binomial=1; unsigned long int number=1; for(scanf("%lu",&t);t>0;t--) { scanf("%lu%lu",&n,&k); if(k<(n/2)) k=n-k; for(binomial=1,number=1;n>k;k++) { binomial=binomial*(k+1)/number; number++; } printf("%lu\n",binomial); } return 0; } It works fine for the example input, but the solution is judged via a problem site (http://www.spoj.pl/SHORTEN/problems/BINOMIAL/english/) and the solution is not accepted. I tried other inputs too and all of them gave back the right output. My question is: Is there a reason why this solution is invalid?
As 1000C500 is around 300 digits, it cant be stored in an unsigned long. In short, you need to start over and think of a better technique.