Find the most frequent strings in an array in C - 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;
}

Related

Im trying to put return at the end of the code but they keep messing me up with while before return

Like i want to put the "return 0" at the right place, everything in the code is working normal.
complie the code and they keep saying about "while before return", i tried to put outside of the code and inside
#include <stdio.h>
#include <stdlib.h>
#define MAXN 100
int menu(){
printf("Menu:\n");
printf("1- Add a value\n");
printf("2- Search a value\n");
printf("3- Print out the array\n");
printf("4- Print out values in a range\n");
printf("5- Print out the array in ascending order\n");
printf("6- Quit?\n");
printf("Enter your operation: ");
int choice;
scanf("%d", &choice);
return choice;
}
int isFul (int*a, int n){
return n==MAXN;
}
int isEmpty (int*a, int n){
return n==0;
}
void add(int value, int*a, int*pn){
a[*pn] = value;
(*pn)++;
}
int search(int x, int *a, int n){
int i;
for (i=0; i<n; i++) if (a[i]==x) return i;
return -1;
}
void printvalueinrange (int*a, int n){
int i, min, max;
printf("\nEnter min value: ");scanf("%d", &min);
printf("\nEnter max value: ");scanf("%d", &max);
for(i=0; i<sizeof(a); i++)
if(a[i]>=min&&a[i]<=max) printf("%d", a[i]);
}
void printarray(int*a, int n){
int i;
for (i=0;i<n;i++) printf("%d", a[i]);
}
void printAsc(int*a, int n){
int** adds =(int**)calloc(n, sizeof(int*));
int i,j;
for(i=0; i<n; i++) adds[i]= &a[i];
int* t;
for (i=0;i<n-1; i++)
for(j=n-1; j>i; j--)
if (*adds[j]< *adds[j-i]){
t=adds[j];
adds[j]=adds[j-1];
adds[j-1]=t;
}
for (i=0;i<n; i++) printf("%d ", *adds[i]);
free(adds);
}
int main(){
int a[MAXN];
int n=0;
int value;
int choice;
do
{ choice= menu();
switch(choice){
case 1:{
if (isFull(a,n)) printf("\n Sorry! The array is full.\n");
else
{ printf ("Input an added value:");
scanf("%d", &value);
add(value, a, &n);
printf("Added!\n");
}
break;
}
case 2:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{ printf ("Input the searched value:");
scanf("%d", &value);
int pos = search(value, a, n);
if (pos<0) printf("Not found!\n");
else printf("Postion is found: %d\n", pos);
} break;
}
case 3:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printarray(a,n);
} break;
}
case 4:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printvalueinrange(a,n);
} break;
}
case 5:{
if (isEmpty(a,n)) printf("\n Sorry! The array is empty.\n");
else
{
printAsc(a,n);
} break;
}
default: printf ("Goodbye!");
break;
}
while (choice>0 && choice<7);
getchar();
}
return 0;
}
I just adding some word to fit with the requirement :"D
And if u find out any kind of error or mistake that in my code, just points out for me to improve them xd
In this part of main
}
while (choice>0 && choice<7);
getchar();
}
return 0;
}
the while construction occupies a wrong place.
There should be
}
} while (choice>0 && choice<7);
getchar();
return 0;
}
Also in this statement
if (isFull(a,n)) printf("\n Sorry! The array is full.\n");
^^^^^^
there is a typo. There should be
if (isFul(a,n)) printf("\n Sorry! The array is full.\n");

Does Scanf work for large number of inputs

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++;
}
}

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;
}

creation of 2D double array from a text file

I want to write a program to create a 2D double array from a text file. The contents and format of my file is
0,0.23645,8.457
4.125,7.102,8.102
1.036,0.547,3.2298,
Same number of row and same number of column. Each number is differentiated by one ','. Each line is differentiated by one'\n' character and at the end one comma.
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *myfile
float a,data[10][10];
char ch;
int i,j;
clrscr();
myfile=fopen("E:\\input.txt", "r");
for(i = 0; i<3; i++)
{
for (j = 0 ; j<3; j++)
{
fscanf(myfile,"%f %c",&a,&ch);
printf("%f ",a);
data[i][j]=a;
}
printf("\n");
}
fclose(myfile);
printf("\n");
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
{ printf("%f ",data[i][j]);
}
printf("\n");
}
getch();
}
It is working, but not for every time. Sometimes it is giving garbage values, zeros or NAN. I don't know why the result of same code gives sometimes correct or sometimes wrong values?
In this code, I have made my number of row/column constant. But if it is unknown then how do I do? So, I have written another code.
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *myfile;
float a,data[10][10];
char ch;
int i,j,line=0,target=0;
int f=getc(myfile);
clrscr();
myfile=fopen("E:\\input.txt", "r");
while (f!= EOF)
{ if(f=='\n')
line++;
f=getc(myfile);
}
//printf("%d",line);
target=++line;
//printf("%d",target);
for(i = 0; i<target; i++)
{
for (j = 0 ; j<target; j++)
{
fscanf(myfile,"%f %c",&a,&ch);
printf("%f ",a);
data[i][j]=a;
}
printf("\n");
}
fclose(myfile);
printf("\n");
for(i=0;i<target;i++)
{ for(j=0;j<target;j++)
{ printf("%f ",data[i][j]);
}
printf("\n");
}
getch();
}
But it is not at all working, everytime is giving wrong values.
Kindly help
It's done with the following code.
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *myfile;
float a,data[10][10];
char ch;
int i=0,j=0,target=-1;
clrscr();
myfile=fopen("E:\\input.txt", "r");
while(1)
{
while(1)
{ fscanf(myfile,"%f %c",&a,&ch);
data[i][j]=a;
j++;
if(ch=='0')
{ target=j;
break;
}
else if(j==target)
break;
}
j=0;
i++;
if(i==target)
break;
}
fclose(myfile);
for(i=0;i<target;i++)
{ for(j=0;j<target;j++)
{ printf("%f ",data[i][j]);
}
printf("\n");
}
getch();
}
removed the EOF, rather using while loop.

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