I am writing a program that receive maximum of 25 line of string each has 20 characters maximum. The program will print the smallest and the largest string. However the following program gives me Segmentation fault (core dumped) :(( It looks simple but i have no idea what went wrong....
Can anyone help me out?? -
#include<stdio.h>
-
#include<stdlib.h>
-
#include<string.h>
-
-
#define MAX_INPUT 25
-
#define MAX_CHAR 20
-
-
int main(void)
-
{
-
int t, i, j;
-
-
char text[MAX_INPUT][MAX_CHAR];
-
-
char smallest[0][MAX_CHAR], largest[0][MAX_CHAR];
-
-
printf("Enter 4 characters to quit. \n");
-
-
for(t=0; t<MAX_INPUT; t++)
-
{
-
printf("Enter word %d: ", t);
-
gets(text[t]);
-
if(strlen(text[t])==4) break;
-
}
-
-
-
for(i=0; i<t; i++)
-
{
-
for(j=0; text[i][j]; j++)
-
{
-
if(text[i][j]<smallest[0][MAX_CHAR])
-
{
-
text[i][j]=smallest[0][MAX_CHAR];
-
}
-
if(text[i][j]>largest[0][MAX_CHAR])
-
{
-
text[i][j]=largest[0][MAX_CHAR];
-
}
-
}
-
}
-
-
printf("Smallest word: %s\n", smallest[0][MAX_CHAR]);
-
printf("Largest word: %s\n", largest[0][MAX_CHAR]);
-
-
return EXIT_SUCCESS;
-
}
-
8 12147
You don't have to remember all those strings; all you have to remember are the smallest and largest strings so far. After having read the first string that string is the smalles and largest string so far.
kind regards,
Jos
@JosAH
Sorry Jos, what do you mean the first string is the smallest and largest string so far??
@crystal2005
Suppose you haven't read any strings yet; you don't know what the smallest or largest strings so far are; suppose you have read one string; it has to be the smallest and largest string read so far.
kind regards,
Jos
text[i][j]=smallest[0][MAX_CHAR];
What do you try to accomplish here?
@newb16
I want to store the smallest array of string. But the code doesn't seem that way.
char largest[0][MAX_CHAR] is a wrong way to store string - sizeof(largest) on my machine returns 0, because it's an array of zero arrays of N chars. However largest[0][MAX_CHAR] is located at 25 bytes from its beginning, overlapping some other variable. Also, you never assign anything to 'smallest' but print it after all.
After trying for quite sometime, it does work... but my code only take one character comparison in front. Any suggestion to compare probably up to 2 or 3 characters for each string?? -
#include<stdio.h>
-
#include<stdlib.h>
-
#include<string.h>
-
-
#define MAX_INPUT 25
-
#define MAX_CHAR 20
-
-
int main(void)
-
{
-
int t, i;
-
-
char text[MAX_INPUT][MAX_CHAR];
-
-
char* largestString;
-
char* smallestString;
-
unsigned int largestStringASCII;
-
unsigned int smallestStringASCII;
-
-
unsigned int largestValue = 0;
-
unsigned int smallestValue = 127; //ASCII max No.
-
-
printf("Enter 4 characters to quit. \n");
-
-
for(t=0; t<MAX_INPUT; t++)
-
{
-
printf("Enter word %d: ", t);
-
gets(text[t]);
-
if(strlen(text[t])==4) break;
-
}
-
-
-
for(i=0; i<=t; i++)
-
{
-
largestStringASCII = text[i][0];
-
if(largestStringASCII>largestValue)
-
{
-
largestValue = largestStringASCII;
-
largestString = text[i];
-
}
-
}
-
-
for(i=0; i<=t; i++)
-
{
-
smallestStringASCII = text[i][0];
-
if(smallestStringASCII<smallestValue)
-
{
-
smallestValue = smallestStringASCII;
-
smallestString = text[i];
-
}
-
}
-
-
printf("Smallest word: %s\n", smallestString);
-
printf("Largest word: %s\n", largestString);
-
-
return EXIT_SUCCESS;
-
}
-
Why don't you use the strcmp( ... ) function? It was made for these purposes.
kind regards,
Jos
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Keke922 |
last post by:
I have to write a program that allows the user to enter a series of integers
and -99 when they want to exit the loop. How do I display the largest and
smallest number the user entered?
|
by: Code4u |
last post by:
I need to write an algorithm that sheds the outliers in a large data
set, for example, I might want to ignore the smallest 2% of values and
find the next smallest. Boost has a nth_element...
|
by: dmsy |
last post by:
Is there a way to use javascript to increase the overall document text
size, just like if you click View->Text Size->Largest, Larger, Medium,
Smaller, Smallest? This is on IE browser.
|
by: Peter Ammon |
last post by:
I have a floating point number. I'd like to get the nearest floating
point number that is larger or smaller than the given number. I
investigated FLT_EPSILON but it only seems to be useful if the...
|
by: ramu |
last post by:
Hi,
I have, suppose 1000 numbers, in a file. I have to find out 5
largest numbers among them without sorting. Can you please give me an
efficient idea to do this? My idea is to put those numbers...
|
by: HEMH6 |
last post by:
Who can help solve this problem???
Finding the Largest Value
(a) write a function, largest(), that returns the largest value in a signed integer array. The array and its size are passed as...
|
by: Subra |
last post by:
Hi,
What is the best way to find the 1000 largest numbers from the file
having hell lot of entries ? Can you please help me to find out the
way ? Do I need to go for B+ trees ??
Please help,...
|
by: MBeckford05 |
last post by:
Hello, I am trying to write java program to input three integer numbers compare them and display the largest and Smallest number. I have thought about the problem I would need to use a comparison...
|
by: tom |
last post by:
Hi!
How can I determine the smallest and largest values of numeric types
(for example int) possible in my system? I think there exists a function
for this task but I don't know it.
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |