hi. i need help with one of my assignment.
i need to store stdin doubles in an array.at the same time if the user enter the return key twice(meaning once after the number and another after) the program must stop its input automatically. however the user may enter any value and can have up to 1000 numbers. at the same time i should be able to redirect to a text file document with numbers when i compile it and the numbers in the file should act just like stdin. if fopen fclose is used it is being done wrong.
eg. a.out -gt </usr/units/ep100/ass_sup.062/test_values1
gt: the function
test_value1: text file with numbers on each line
17 1720 RedSon 5,000
Recognized Expert Expert
Post your code so we can see what you are doing. Please point out where the errors occur in your code, to make it easier for us to spot.
Post your code so we can see what you are doing. Please point out where the errors occur in your code, to make it easier for us to spot.
- int main(int argc, char * argv[])
-
{
-
FILE * input = stdin;
-
FILE * output = stdout;
-
-
int i;
-
int number_of_value=-1;
-
double minn;
-
double min1;
-
double maxx;
-
double max1;
-
double average;
-
double sum=0;
-
int A=0;
-
int B=0;
-
double greater_value=0;
-
-
for (i=1;i<argc;i++)
-
{
-
if (strcmp(argv[i],"-help") == 0)
-
{
-
fprintf(output,"######################### Data_Message.c #########################\n\n");
-
fprintf(output,"**************************-HELP function**************************\n");
-
fprintf(output,"if -max is specified, the maximum value will be output\n");
-
fprintf(output,"If -min is specified, the minimum value will be output\n");
-
fprintf(output,"If -average is specified, then the average of the values will be output\n");
-
fprintf(output,"If -gta is specified, the number of values greater than the average is output\n");
-
fprintf(output,"If -gt xxx is specified, the number of values greater than the value xxx is output\n");
-
fprintf(output,"To execute any of these functions the user must compile the program using 'cc Data_Massage.c'\n");
-
fprintf(output,"Once the program is compiled then 'a.out xxxx(functions)' can be used to run the program\n");
-
return 0;
-
}
-
}
-
-
if (argc<2)
-
{
-
fprintf(output,"NOT ENOUGH CLAs\n");
-
exit (0);
-
}
-
-
fprintf(output,"Please enter total number of values:");
-
fscanf(input,"%d",&number_of_value);
-
-
double sorted[number_of_value+1];
-
-
for (i=0;i<number_of_value;i++)
-
{
-
fprintf(output,"Please enter values: ");
-
-
fscanf(input,"%lf",&sorted[i]);
-
} // this is the bit with the problem. i need to make it so that it can store numbers into an array from a text file full of numbers. at the same time if the user choose to input numbers the program should understand to stop when the return key is pressed twice.
-
-
for (i=1;i<argc;i++)
-
{
-
if (strcmp(argv[i],"-min")==0)
-
{
-
minn=sorted[0];
-
for (i=1;i<number_of_value;i++)
-
{
-
min1=sorted[i];
-
if (minn>min1)
-
{
-
minn=min1;
-
}
-
}
-
fprintf(output,"MIN VALUE IS: %lf\n",minn);
-
}
-
}
-
-
-
for (i=1;i<argc;i++)
-
{
-
if (strcmp(argv[i],"-max")==0)
-
{
-
maxx=sorted[0];
-
for (i=1;i<number_of_value;i++)
-
{
-
max1=sorted[i];
-
if (maxx<max1)
-
{
-
maxx=max1;
-
}
-
}
-
fprintf(output,"MAX VALUE IS: %lf\n",maxx);
-
}
-
}
-
-
for (i=1;i<argc;i++)
-
{
-
if (strcmp(argv[i],"-average")==0)
-
{
-
for (i=0;i<number_of_value;i++)
-
{
-
sum=sum+sorted[i];
-
}
-
average=(sum/number_of_value);
-
fprintf(output,"AVERAGE IS: %lf\n",average);
-
}
-
}
-
-
for (i=1;i<argc;i++)
-
{
-
if (strcmp(argv[i],"-gta")==0)
-
{
-
for (i=0;i<number_of_value;i++)
-
{
-
sum=sum+sorted[i];
-
}
-
average=(sum/number_of_value);
-
-
for (i=0;i<number_of_value;i++)
-
{
-
if (sorted[i]>average)
-
A++;
-
}
-
fprintf(output,"The number of numbers larger then the average is: %d\n",A);
-
}
-
}
-
for (i=1;i<argc;i++)
-
{
-
if (strcmp(argv[i],"-gt")==0)
-
{
-
greater_value=atof(argv[i+1]);
-
for (i=0;i<number_of_value;i++)
-
{
-
if (sorted[i]>greater_value)
-
B++;
-
}
-
fprintf(output,"The number of numbers larger then the value is: %d\n",B);
-
return 0;
-
}
-
}
-
}
the text file look like this
2
22.63
22.33
1
-200.2
....
AdrianH 1,251
Recognized Expert Top Contributor
First off, break it down into functions. All of this in one function is crazy. It makes it hard to manage and read. If you write things like this later, you will be pulling your hair out as will those around you who need to go through your code. Broken down, you could send just the relevant code.
// This is not portable, as far as I know it is a gnu extension. Not necessarily wrong, but you should be aware of it.
double sorted[number_of_value+1];
// This is not bad. I'd personally put the fprint outside, but it doesn't really matter. I'd also put in your test i<number_of_value && !feof(input)
for (i=0;i<number_of_value;i++)
{
fprintf(output,"Please enter values: ");
fscanf(input,"%lf",&sorted[i]);
}
As for the double return issue. Define char returnsReceived[5]={} and use fscanf(input, "%4[^\n\r]", returnsReceived); after your fscanf(). Then check returnsReceived for two '\n' characters. In Unix they will appear one after the other, in Windows they will appear with a '\r' between them. Add the result of your test for two '\n''s as part of your exit test.
Hope this helps.
Adrian
First off, break it down into functions. All of this in one function is crazy. It makes it hard to manage and read. If you write things like this later, you will be pulling your hair out as will those around you who need to go through your code. Broken down, you could send just the relevant code.
// This is not portable, as far as I know it is a gnu extension. Not necessarily wrong, but you should be aware of it.
double sorted[number_of_value+1];
// This is not bad. I'd personally put the fprint outside, but it doesn't really matter. I'd also put in your test i<number_of_value && !feof(input)
for (i=0;i<number_of_value;i++)
{
fprintf(output,"Please enter values: ");
fscanf(input,"%lf",&sorted);
}
As for the double return issue. Define char returnsReceived[5]={} and use fscanf(input, "%4[^\n\r]", returnsReceived); after your fscanf(). Then check returnsReceived for two '\n' characters. In Unix they will appear one after the other, in Windows they will appear with a '\r' between them. Add the result of your test for two '\n''s as part of your exit test.
Hope this helps.
Adrian
hi this is the first time have done c and i aren't familiar with some of your codes. !feof(input) what does this mean.
char returnsReceived[5]={} however if there can be any amount of numbers in the text file up 1000 numbers. what does the {} mean after the equal sign
fscanf(input, "%4[^\n\r]", where do i put this and what does %4 mean
i know you can use functions to make it easier but this is how i have to summited it
RedSon 5,000
Recognized Expert Expert
hi this is the first time have done c and i aren't familiar with some of your codes. !feof(input)[/i] what does this mean.
char returnsReceived[5]={} however if there can be any amount of numbers in the text file up 1000 numbers. what does the {} mean after the equal sign
fscanf(input, "%4[^\n\r]", where do i put this and what does %4 mean
i know you can use functions to make it easier but this is how i have to summited it
steve, it might be better for Adrian to point you to a API list so that you can get the entire syntax of those methods. The bracket after returnsReceived indicate to the compiler to make each element of the array NULL when the object is created. If you take a look at the API set you can see what the %4 is for in fscanf. If you tell me what OS and what compiler you are using I could try to find some online API resources for you.
steve, it might be better for Adrian to point you to a API list so that you can get the entire syntax of those methods. The bracket after returnsReceived indicate to the compiler to make each element of the array NULL when the object is created. If you take a look at the API set you can see what the %4 is for in fscanf. If you tell me what OS and what compiler you are using I could try to find some online API resources for you.
using fedora unix
AdrianH 1,251
Recognized Expert Top Contributor
hi this is the first time have done c and i aren't familiar with some of your codes. !feof(input)[/i] what does this mean.
char returnsReceived[5]={} however if there can be any amount of numbers in the text file up 1000 numbers. what does the {} mean after the equal sign
fscanf(input, "%4[^\n\r]", where do i put this and what does %4 mean
i know you can use functions to make it easier but this is how i have to summited it
feof(input) states if you have reached the end of file in the input FILE. The ! means not. Thus, !feof(input) means you have not reached the end of the input FILE.
char returnsReceived[5]={}; // Allocate an array of 5 chars and set each element to '\0' (i.e. null)
%[\n\r] means that you want all characters that are inside of the []. This consists of \n (newline char) and \r (carriage return char). Putting a 4 after the % means I want at most 4 of them. If you get less than 4, the string will be terminated with a '\0', if you get 4, then it will not be terminated with a '\0', that is why I allocated 5, to ensure that it is terminated with a '\0'.
I had actually ment to use the format string "%4[\n\r]", if you put a ^ right after the [ it means anything but the characters in the [], which is not what you wanted.
So after your fscanf(input,"%lf",&sorted[i]); you would then check for the carriage returns by reading them in and counting them. If it is found that you have two returns, exit by setting a Boolean var which you will add in your loop test. I.e. -
bool found2CRs = false; // C++
-
int found2CRs = 0; /* C */
-
for (...; <other tests> && !found2CRs; ...)
-
Or if you want to be sloppy, use a break.
Does this make sense?
Adrian
RedSon 5,000
Recognized Expert Expert
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/ This looks pretty comprehensive, see if this reference helps too.
hi thanks for the help. but i got one last question which is that if the text file has different amount of numbers ie: might be 5 or 10 or 45 numbers, how do i read the file first to see how many numbers there are in a file before declaring the size of my array, with out using fopen and close
a.out -gt </usr/units/ep100/ass_sup.062/test_values1
this is the command that i have to use for my work. does < mean redirect
AdrianH 1,251
Recognized Expert Top Contributor
hi thanks for the help. but i got one last question which is that if the text file has different amount of numbers ie: might be 5 or 10 or 45 numbers, how do i read the file first to see how many numbers there are in a file before declaring the size of my array, with out using fopen and close
a.out -gt </usr/units/ep100/ass_sup.062/test_values1
this is the command that i have to use for my work. does < mean redirect
You know the maximum right? So declare an array that is the max number of elements.
Adrian
well it can go up to max of 1000. what happens if there are only 800 numbers. what else do i do with the other 200 space
AdrianH 1,251
Recognized Expert Top Contributor
well it can go up to max of 1000. what happens if there are only 800 numbers. what else do i do with the other 200 space
Ignore them. :) You just keep count of how many you use and ignore the rest.
Otherwise, you could start using container classes like STL. They will handle the reallocation of memory for you. But based on what you are doing and the level that you are doing it at, ignoring them should be fine.
Adrian
AdrianH 1,251
Recognized Expert Top Contributor
thank you so much
No prob, glad to help.
Adrian
- while ((i<1000) && (found2CRs==false))
-
{
-
fscanf(input, "%4[\n\r]",&unsorted[i]);
-
x++;
-
found2CRs=true;
-
number_of_value ++;
-
fprintf(output,"number of value %d", unsorted[i]);
-
}
the idea of this code is to scan a bunch of numbers from a text file and store it in an array. at the same time once it is in an array i have to resort it so that the array is the precise size. this means that the text file might contain up to 1000 numbers. so when i read the file i also have to find out how many numbers there are to declare my array size. in addition, i am not ment to use fopen and close. when i execuate the program <(symbole) is suppose to redirect to the file and it should act just like a stdin. also when two return keys are found in a row the program should exit it input stage. without using files what else can i do
AdrianH 1,251
Recognized Expert Top Contributor - while ((i<1000) && (found2CRs==false))
-
{
-
fscanf(input, "%4[\n\r]",&unsorted[i]);
-
x++;
-
found2CRs=true;
-
number_of_value ++;
-
fprintf(output,"number of value %d", unsorted[i]);
-
}
the idea of this code is to scan a bunch of numbers from a text file and store it in an array. at the same time once it is in an array i have to resort it so that the array is the precise size. this means that the text file might contain up to 1000 numbers. so when i read the file i also have to find out how many numbers there are to declare my array size. in addition, i am not ment to use fopen and close. when i execuate the program <(symbole) is suppose to redirect to the file and it should act just like a stdin. also when two return keys are found in a row the program should exit it input stage. without using files what else can i do
Yes, we've been over this already. Do you have a question?
Adrian
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Donald Tyler |
last post by:
Then the only way you can do it that I can think of is to write a PHP script
to do basically what PHPMyAdmin is trying to do but without the LOCAL in
there.
However to do that you would need to...
|
by: Neil Ginsberg |
last post by:
We're using SQL Server 7 with an Access 2000 MDB as a front end with ODBC
linked tables. I recently created a new set of tables for the app, and users
are complaining that unsaved data is being...
|
by: juky |
last post by:
Hi all,
I have 2 applications one in VB.net and the other in VC6. I need to
pass data between them. How can I do it? what's the best way to
implement such communication ?
Any comment will be...
|
by: Leszek Taratuta |
last post by:
Hello,
I have several drop-down lists on my ASP.NET page. I need to keep data
sources of these lists in Session State.
What would be the most effective method to serialize this kind of data...
|
by: William |
last post by:
After much frustration I was able to update my data store via code only.
Using the data adapter was the only way I was able to set up all the objects
written in my code. Basically, I cheated by...
| |
by: MR |
last post by:
I get the following Exception "The data at the root level is invalid. Line
1, position 642" whenever I try to deserialize an incoming SOAP message. The
incoming message is formed well and its...
|
by: |
last post by:
Hello,
Does anyone have an idea on how I can filter the data in the gridview
control that was returned by an sql query?
I have a gridview that works fine when I populate it with data. Now I...
|
by: Macca |
last post by:
Hi,
I am writing an asychronous socket server to handle 20+ simulataneous
connections.
I have used the example in MSDN as a base. The code is shown at end of
question.
Each connection has a...
|
by: Charles Law |
last post by:
Here's one that should probably have the sub-heading "I'm sure I asked this
once before, but ...".
Two users are both looking at the same data, from a database. One user
changes the data and...
|
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: 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,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |