473,507 Members | 3,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

data message

8 New Member
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
Feb 13 '07 #1
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.
Feb 13 '07 #2
st3v3
8 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char * argv[])
  2. {
  3. FILE * input = stdin;
  4. FILE * output = stdout;
  5.  
  6. int i;
  7. int number_of_value=-1;
  8. double minn;
  9. double min1;
  10. double maxx;
  11. double max1;
  12. double average;
  13. double sum=0;
  14. int A=0;
  15. int B=0;
  16. double greater_value=0;
  17.  
  18.     for (i=1;i<argc;i++)
  19.     {
  20.         if (strcmp(argv[i],"-help") == 0)
  21.         {
  22.             fprintf(output,"######################### Data_Message.c #########################\n\n");
  23.             fprintf(output,"**************************-HELP function**************************\n");
  24.             fprintf(output,"if -max is specified, the maximum value will be output\n");
  25.             fprintf(output,"If -min is specified, the minimum value will be output\n");
  26.             fprintf(output,"If -average is specified, then the average of the values will be output\n");
  27.             fprintf(output,"If -gta is specified, the number of values greater than the average is output\n");
  28.             fprintf(output,"If -gt xxx is specified, the number of values greater than the value xxx is output\n");
  29.             fprintf(output,"To execute any of these functions the user must compile the program using 'cc Data_Massage.c'\n");
  30.             fprintf(output,"Once the program is compiled then 'a.out xxxx(functions)' can be used to run the program\n");
  31.         return 0;
  32.         }
  33.     }
  34.  
  35.         if (argc<2)
  36.         {
  37.             fprintf(output,"NOT ENOUGH CLAs\n");
  38.         exit (0);
  39.         }
  40.  
  41.         fprintf(output,"Please enter total number of values:");
  42.         fscanf(input,"%d",&number_of_value);
  43.  
  44. double sorted[number_of_value+1];
  45.  
  46.         for (i=0;i<number_of_value;i++)
  47.         {
  48.             fprintf(output,"Please enter values: ");
  49.  
  50.             fscanf(input,"%lf",&sorted[i]);
  51.         } // 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.
  52.  
  53.     for (i=1;i<argc;i++)
  54.     {
  55.         if (strcmp(argv[i],"-min")==0)
  56.         {
  57.             minn=sorted[0];
  58.             for (i=1;i<number_of_value;i++)
  59.             {
  60.                 min1=sorted[i];
  61.                 if (minn>min1)
  62.                 {
  63.                     minn=min1;
  64.                 }
  65.             }    
  66.             fprintf(output,"MIN VALUE IS: %lf\n",minn);
  67.         }
  68.     }
  69.  
  70.  
  71.     for (i=1;i<argc;i++)
  72.     {
  73.         if (strcmp(argv[i],"-max")==0)
  74.         {    
  75.             maxx=sorted[0];
  76.             for (i=1;i<number_of_value;i++)
  77.             {
  78.                 max1=sorted[i];
  79.                 if (maxx<max1)
  80.                 {
  81.                     maxx=max1;
  82.                 }
  83.             }
  84.             fprintf(output,"MAX VALUE IS: %lf\n",maxx);
  85.         }
  86.     }
  87.  
  88.     for (i=1;i<argc;i++)
  89.     {
  90.         if (strcmp(argv[i],"-average")==0)
  91.         {
  92.             for (i=0;i<number_of_value;i++)
  93.             {
  94.                 sum=sum+sorted[i];
  95.             }
  96.             average=(sum/number_of_value);
  97.             fprintf(output,"AVERAGE IS: %lf\n",average);
  98.         }
  99.     }
  100.  
  101.     for (i=1;i<argc;i++)
  102.     {
  103.         if (strcmp(argv[i],"-gta")==0)
  104.         {
  105.             for (i=0;i<number_of_value;i++)
  106.             {
  107.                 sum=sum+sorted[i];
  108.             }
  109.             average=(sum/number_of_value);
  110.  
  111.                 for (i=0;i<number_of_value;i++)
  112.                 {
  113.                     if (sorted[i]>average)
  114.                     A++;
  115.                 }
  116.             fprintf(output,"The number of numbers larger then the average is: %d\n",A);
  117.         }
  118.     }
  119.     for (i=1;i<argc;i++)
  120.     {
  121.         if (strcmp(argv[i],"-gt")==0)
  122.         {
  123.             greater_value=atof(argv[i+1]);
  124.             for (i=0;i<number_of_value;i++)
  125.             {
  126.                 if (sorted[i]>greater_value)
  127.                 B++;
  128.             }
  129.             fprintf(output,"The number of numbers larger then the value is: %d\n",B);
  130.         return 0;
  131.         }
  132.     }
  133. }


the text file look like this
2
22.63
22.33
1
-200.2
....
Feb 13 '07 #3
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
Feb 13 '07 #4
st3v3
8 New Member
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
Feb 13 '07 #5
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.
Feb 13 '07 #6
st3v3
8 New Member
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
Feb 13 '07 #7
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.
Expand|Select|Wrap|Line Numbers
  1. bool found2CRs = false; // C++
  2. int found2CRs = 0; /* C */
  3. for (...; <other tests> && !found2CRs; ...)
  4.  
Or if you want to be sloppy, use a break.

Does this make sense?


Adrian
Feb 13 '07 #8
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.
Feb 13 '07 #9
AdrianH
1,251 Recognized Expert Top Contributor
http://www.acm.uiuc.edu/webmonkeys/book/c_guide/ This looks pretty comprehensive, see if this reference helps too.
I like your link in that everything on one page. You can also find it here at http://www.cplusplus.com/reference/c...dio/scanf.html


Adrian

PS the [url] tag can be used to encompass a url or by using [url=address] you can encompass anything with a link to the address.
Feb 13 '07 #10
st3v3
8 New Member
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
Feb 13 '07 #11
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
Feb 13 '07 #12
st3v3
8 New Member
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
Feb 13 '07 #13
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
Feb 13 '07 #14
st3v3
8 New Member
thank you so much
Feb 13 '07 #15
AdrianH
1,251 Recognized Expert Top Contributor
thank you so much
No prob, glad to help.


Adrian
Feb 13 '07 #16
st3v3
8 New Member
Expand|Select|Wrap|Line Numbers
  1. while ((i<1000) && (found2CRs==false))
  2.         {
  3.             fscanf(input, "%4[\n\r]",&unsorted[i]);
  4.             x++;
  5.             found2CRs=true;
  6.             number_of_value ++;
  7.             fprintf(output,"number of value %d", unsorted[i]);    
  8.         }
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
Feb 14 '07 #17
AdrianH
1,251 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. while ((i<1000) && (found2CRs==false))
  2.         {
  3.             fscanf(input, "%4[\n\r]",&unsorted[i]);
  4.             x++;
  5.             found2CRs=true;
  6.             number_of_value ++;
  7.             fprintf(output,"number of value %d", unsorted[i]);    
  8.         }
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
Feb 14 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

0
6669
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...
32
3190
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...
3
4049
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...
13
5210
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...
4
1824
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...
9
6607
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...
7
14784
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...
0
4649
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...
30
3354
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...
0
7221
marktang
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,...
0
7109
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...
0
7313
Oralloy
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,...
0
5619
agi2029
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,...
1
5039
isladogs
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...
0
4702
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1537
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 ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.