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 12181 JosAH 11,448
Recognized Expert MVP
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??
JosAH 11,448
Recognized Expert MVP @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;
-
}
-
JosAH 11,448
Recognized Expert MVP
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 algorithm, however, it
partially sorts the data. I have a requirement that the data remain in
the orginal order. Of course I could make a copy of the vector or
array and then apply nth_element, but this would be expensive in
memory and would require...
|
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 given
number is 1. Any suggestions?
Thanks,
-Peter
|
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 into a
binary tree and to find the largest numbers. How else can we do it?
Regards
| |
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 arguments.
(b)Write a main program that inputs MAX values from the keyboard into a signed
integer array, array, and points, using largest(), the largest value to the screen.
|
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,
Subramanya M
|
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 operate determine which number is larger and which is smaller. I am new to java I was wondering how to but this into java source code.
Can anyone Help?
Thank You.
MBeckfod05
|
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: 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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |