Does Scanf work for large number of inputs - c

Here's description for code below. For the input sequence of numbers from 1 to 3000 my code stops at 1040 input. I can't understand why it is happening. Please help!!
#include<stdio.h>
#include<stdlib.h>
void search(int x,int *array,int a,int b,int *c)
{
for(int i=b+1;*(array+i)<=x;i++)
{
if(*(array+i)==x)
{
*c=1;
printf("%d %d %d\n",*(array+b),x,(*(array+b)+x));
return;
}
}
}
int main()
{
int t,n,i=1,j,sum,flag;
scanf("%d",&t);
getchar();
while(i<=t)
{
flag=0;
scanf("%d",&n);
printf("%d\n",n);
getchar();
int *array=(int *)malloc(n*sizeof(int));
for(j=0;j<n;j++)
{
scanf("%d",(array+j));
getchar();
printf("%d\n",*(array+j));
}
scanf("%d",&sum);
getchar();
for(j=0;*(array+j)<=(sum/2);j++)
{
search((sum-*(array+j)),array,n,j,&flag);
}
if(flag==0)
{
printf("-1\n");
}
i++;
}
}

Related

Why doesn't this c++ code template work? I'm having trouble

#include <stdio.h>
int main()
{
int list[10];
int a,b,c;
int d=1;
int e=0;
printf("Enter a starting number to find 10 prime numbers: ");
scanf("%d",&a);
printf("Enter a ending number to find 10 prime numbers: ");
scanf("%d",&b);
for(c=0;c<10;c++)
{
while(a<b)
{
while(d<a)
{
if(a%d==0)
{
e=1;
}
d++;
}
a++;
}
if(e==0)
{
list[c]=a;
printf("%d is prime.",list[c]);
}
}
}
The code that will add 10 prime numbers in the given range to the list arr and print them. Why doesn't it work? Thanks. Please explain.
to flesh out #Craig answer
for(c=0;c<10;)
{
while(a<b)
{
while(d<a)
{
if(a%d==0)
{
e=1;
}
d++;
}
a++;
}
if(e==0)
{
list[c]=a;
printf("%d is prime.",list[c]);
c++;
}
}

Program that scans begining and ending number of two intervals and compares them

I'm preparing for a programing test at my uni and i'd like to write a program in c which can compare two intervals. I've tried many strategies but nothing seems to quite work properly.I believe there is an issue within my if parameters.
#include <stdio.h>
#include <stdlib.h>
//functions
int io_menu(int a,int b);
int int_check(int k,int n);
int basic_ui(int k,int n,int i,int j);
unsigned interval(int k,int n,int i,int j);
void printint(char str[]);
int main()
{
int fir_beg,fir_last,sec_beg,sec_last;
do{
basic_ui(fir_beg,fir_last,sec_beg,sec_last);
}while(int_check(fir_beg,fir_last)==0 || int_check(sec_beg,sec_last)==0);
return 0;
}
//menu for print and scan
int io_menu(int a,int b)
{
printf("Give Begining number: ");
scanf("%d",&a);
getchar();
printf("Give Ending number: ");
scanf("%d",&b);
getchar();
}
//integer check
int int_check(int k,int n){
if(n > 0 && k > 0){return 1;}
else{return 0;}
}
//main menu
int basic_ui(int k,int n,int i,int j)
{
char frst[]="first";
char scnd[]="second";
printint(frst);
io_menu(k,n);
printint(scnd);
io_menu(i,j);
if (int_check(k,n)==1 && int_check(i,j)==1)
{
printf("Data is correct,please wait\n");
interval(k,n,i,j);
}
else{
printf("Unable to calculate,please try again later\n\n");
}
}
//calculation for the intervals
unsigned interval(int k,int n,int i,int j)
{
if(k!=n && i!=j){
if(n<i)
{
printf("Before");
}
else if(k>j)
{
printf("After");
}
else if(i=k && n>i)
{
printf("Meets");
}
else if(i=n && k<i)
{
printf("Met By");
}
else if(k<i && n>i)
{
printf("Overlaps");
}
else if(k<j && n>j)
{
printf("Overlapped by");
}
else if(k>i && n<j)
{
printf("Strict during");
}
else if(k<i && n>j)
{
printf("Strict Contains");
}
else if(k=i && n<j)
{
printf("Start");
}
else if(k=i && j>n)
{
printf("Started By");
}
else if(n=j && i<k)
{
printf("Finishes");
}
else if(n=j && i>k)
{
printf("Finished by");
}
else if(n=j && i=k)
{
printf("Equal");
}
}
else{printf("Unable to calculate,please try again later\n\n");}
}
void printint (char str[])
{
printf("Enter for %s interval\n",str);
return 0;
}
To be more specific k,n are the beginning and ending numbers for the first interval as well as i,j are for the second one. Please try to understand that it is my 3d week familiarising with the c language as well as with programing in general.

Find the most frequent strings in an array in C

I'm writing a code which displays the most frequent strings in an array. I've written the code but something is wrong. Can you please tell me where is the mistake?
#include <stdio.h>
#include <string.h>
int main(){
int n,i,j,k,count=0, high=1, flag=0;
char words[100][20],wordsOutput[100][20];
printf("Enter number of elements to enter in an array: ");
scanf("%d",&n);
for(i=0;i<n;i++){
fflush(stdin);
printf("%d. ",i+1);gets(words[i]);fflush(stdin);
}
for(i=0;i<n-1;i++){
count=0;
for(j=i+1;j<n;j++){
if(strcmp(words[i],words[j])==0){
count++;
}
}
if(high<count){
high=count;
strcpy(wordsOutput[0],words[i]);
strcpy(wordsOutput[1],"");
}
else if(high == count){
flag=0;
for(k=0;strcmp(wordsOutput[k],"")!=0;k++){
if(strcmp(words[i],wordsOutput[k])==0){
flag=1;
}
}
if(flag==0){
for(k=0;strcmp(wordsOutput[k],"")!=0;k++);
strcpy(wordsOutput[k],words[i]);
strcpy(wordsOutput[++k],"");
}
}
}
for(k=0;strcmp(wordsOutput[k],"")!=0;k++){
puts(wordsOutput[k]);
}
if(strcmp(wordsOutput[0],"")==0){
printf("\nNo coincident words");
}
return 0;
}

Insert and delete in a Heap in c

I have a problem in my program which should prompt the user to enter number and the program will heapify them. The program runs but shows a runtime error after inserting the first number.
I tried to fix it multiple times but in vain.
If anyone could point to the precise error in the code it would be much appreciated. Thanks in advance. =)
Anyways, here's the code:
#include <stdio.h>
void insert_node(int arr[], int max){
if(max<15){
printf("Type the number to insert: ");
scanf("%d", arr[max]);
max++;
}
else
printf("Error. The heap is full!");
}
void printheap(int arr[], int max){
int count;
if(max>=1){
printf("\nPrinting the Heap:");
for(count==0;count<=max;count++)
printf(" %d", arr[count]);
}
}
void heapSort(int arr[], int max) {
int i=0,temp,lc,rc;
while(i!=max-1){
lc=2i+1;
rc=2i+2;
if(arr[i]<arr[lc]){
temp=arr[i];
arr[i]=arr[lc];
arr[lc]=temp;
}
else if(arr[i]<arr[rc]){
temp=arr[i];
arr[i]=arr[rc];
arr[rc]=temp;
}
i=i+1;
}
}
int main(int argc, char *argv[]){
int arr[15];
int max=0;
char ch;
while(ch!='n' && ch!='N'){
printheap(arr,max);
insert_node(arr,max);
if(max>1)
heapSort(arr,max);
printf("\nInsert another key (y:yes/n:no)? ");
scanf("%c", &ch);
}
return 0;
}

trying this merge sort... but it's not working... can anyone find the error?

I'm having a problem with a C program to perform a merge sort on a user-defined array. When I run the program, it's taking the array input, but it abruptly stops after that and shows error 255. This algorithm is from mycodeschool.org. Here's my code:
#include<stdio.h>
#include<stdlib.h>
void merge(int *a, int *left,int *right,int n, int ln, int rn)
{
int i,j,k;
i=j=k=0;
while(i<ln&&j<rn)
{
if(left[i]<=right[j])
{
a[k]=left[i];
i++;
}
else
{
a[k]=right[j];
j++;
}
k++;
}
while(i<ln)
{
a[k]=a[i];
k++; i++;
}
while(j<rn)
{
a[k]=a[j];
k++; j++;
}
}
void mergesort(int *a, int n) // for partition
{
int mid,i,*left,*right;
if(n<=1)
return;
mid=n/2;
left=(int *)malloc(sizeof(int)*mid);
right=(int *)malloc(sizeof(int)*(n-mid));
for(i=0;i<mid;i++)
left[i]=a[i];
for(i=mid;i<n;i++);
right[i-mid]=a[i];
mergesort(left,mid);
mergesort(right,(n-mid));
merge(a,left,right,n,mid,n-mid);
}
int main()
{
int *a,n,i;
printf("Enter the size of array: \t");
scanf("%d",&n);
a=(int *)malloc(sizeof(int)*n);
printf("\nEnter the array elements :\n");
for(i=0;i<n;i++)
scanf("%d",(a+i));
mergesort(a,n);
printf("The sorted array is:\n");
for(i=0;i<n;i++)
printf("%d",*(a+i));
return 0;
}
shoud be
while(i<ln)
{
a[k]=left[i];
k++; i++;
}
while(j<rn)
{
a[k]=right[j];
k++; j++;
}
and
for(i=mid;i<n;i++);
remove ;

Resources