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 1740 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_val ue && !feof(input)
for (i=0;i<number_o f_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_val ue && !feof(input)
for (i=0;i<number_o f_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.ed u/webmonkeys/book/c_guide/ This looks pretty comprehensive, see if this reference helps too.
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 be able to place the PHP file on the
server, and I guess you probably can't do that either. Talk about catch
22...
The only other way I can think of is to install MySQL on a machine you
control, then import the data there using the method I...
|
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 lost when they move to a new
record. This seems to be the case when there are multiple users. When there
is a single user using it, we don't seem to have that problem.
It seems that we had this problem when we first converted from an MDB back
end...
|
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 appreciate.
Thank you.
Juky
|
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
structures?
Thanks for any hints,
Leszek Taratuta
|
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 creating an adapter and then
copy, paste, modify it's code to suite my needs. This was the end result:
Private sub updateTable( )
Dim myData As DataSet
'Command is a form level oledbcommand object
Dim Adapter1 As New OleDbDataAdapter(Command)
| |
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 length is 642 bytes ( I have
appended it to the end of this message).
I suspect that the reason may have something to do with an incorrect
declaration of which class to de-serialize to.
In the attached code I substituted @@@@@@@ in the code below with...
|
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 want
to look at that data and filter it based on what is in it.
I know that this could have been done with data sets and data views in
asp.net 1.1 but how is this done now in asp.net 2.0?
|
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 number of different types of data coming in. I have a
databuffer for each type of data coming in.
|
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 commits it. How does the other user get the updated
view without polling for changes? Is there some sort of callback mechanism
that can be set up on the dataset or connection?
TIA
|
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: 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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: 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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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: 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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |