|
//This program read file *.txt, than 100 float numbers(or int numbers, if they are present)
//write in a float array.Next, finds MIN and MAX element in float array with using pointers,
//write results on the screan and if there is no more numbers, go to end, else repet all again.
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define MAXELEMENTS 100 // MAX numbers of elements
//funkcion for max element in float array
float *biggest(float table[],int number)
{
int counter, counter_max;
float aretheysame=0;
for(counter=0; counter < number; counter++)
{
if (*(table+counter) > aretheysame)
{
aretheysame = *(table+counter);
counter_max = counter;
}
}
return(&*(table+counter_max));
}
//finkcion for min element in float array
float *smallest(float table[],int number)
{
int counter, counter_max=0;
float aretheysame=table[0];
for(counter=0; counter < number; counter++)
{
if (*(table+counter) < aretheysame)
{
aretheysame = *(table+counter);
counter_max = counter;
}
}
return(&*(table+counter_max));
}
main(void)
{
FILE *in;
char inter[255];
int num_elements=0;
float element;
static float table[MAXELEMENTS];
float *small,*big;
printf("Adress and value of max and min element in array\n");
printf("Enter file name : ");
gets(inter);
in= fopen(inter,"r");
if(in == NULL)
{
printf("Error. No file with this name\n");
exit(1);
}
while (fscanf(in,"%f",&element) != EOF)
{
table[num_elements] = element;
num_elements++;
}
if (num_elements > 1)
{
big=biggest(table,num_elements);
printf("\nThe adress of max element is %d",&*big);
printf(", with value %f",*big);
small=smallest(table,num_elements);
printf("\nThe adress of min element is %d",&*small);
printf(", with value %f",*small);
}
else
{
;
}
fclose (in);
getchar();
}
And *.txt file:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2000 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
15 14 13 22 55 2 5
The program find min and max from all numbers in file, but not only from 100.
It must be able to do that with long txt files too, and each 100 numbers is work with number (1. work(for first 100 num), 2. work(for secund 100 num),...)
Where I am doing wrong ? Please, help me !
|
|
|
|
|
If there are more than 100 numbers in the file, you'll need to change the value of MAXELEMENTS . A better solution would be to use a dynamic array. That way you do not have to keep changing and compiling code when the size of the input file changes.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
|
I'm having trouble trying to understand what your problem is. You want to read all numbers and get the largest and smallest? I'm not understanding what you say is going wrong. I would try something like this:
#include <set>
#include <iostream>
using namespace std;
set<int> nums;
ifstream in(filename);
while(in)
{
int n;
in >> n;
nums.insert(n);
}
set<int>::iterator it = nums.begin();
cout << "first = " << (*it) << endl;
int last;
while(it != nums.end())
{
last = (*it);
it++;
}
cout << "last = " << last << endl;
-- Rocky Dean Pulley
|
|
|
|
|
I want to read all numbers and get the largest and smallest, but only from first 100 numbers in array[100], after that the same with another 100 numbers and so on.
|
|
|
|
|
As you are reading in the numbers, if num_elements is a multiple of 100, then call biggest() and smallest() .
Another way would be to read all of the numbers in the file, and call the biggest() and smallest() functions once for each group of 100 numbers, like:
big1 = biggest(table, 100);
big2 = biggest(&table[100], 100);
big3 = biggest(&table[200], 100); Make sense?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
Yes, but how can i write big10489=biggest(&table[1048800], 100); ?
This will take a lot of my time. And i don?t now, if file is so long?
|
|
|
|
|
dr.eu wrote:
Yes, but how can i write big10489=biggest(&table[1048800], 100); ?
Exactly like that. This assumes that you have read at least 1,048,900 numbers from the file.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
In that case, i have to import another counter j:
int j;
bigj=biggest(&table[j], 100);
Is right so?
|
|
|
|
|
If not using STL at all is an option, you might ask yourself why you store the numbers in an array in the first place. Just keep track of the smallest and largest numbers you fscanf'ed so far.
|
|
|
|
|
it sounds like he has to for a homework assignment. It sounds like a pretty lame homework assignment, must have been a math teacher turned programming teacher.
-- Rocky Dean Pulley
|
|
|
|
|
About homework you have right ! About teacher ... Who nows ?
Any way could you help ?
|
|
|
|
|
I can't really do your homework for you, you won't learn anything that way. Besides, you are 90% there, just take the time and figure it out, you're almost done.
-- Rocky Dean Pulley
|
|
|
|
|
Yes, from that almost i am gona crazy. The result is O.K. (for all numbers in file). Help me please with 100, 200, ... elements in array !!!
|
|
|
|
|
just use the same array, once you hit a number where (current_number % 100 == 0) then do your biggest/smallest check and reset current_number to 0, keep going on after that and it will just reuse your 100 item array.
-- Rocky Dean Pulley
|
|
|
|
|
I done so:
while (fscanf(in,"%f",&element) != EOF)
{
table[num_elements] = element;
current_number=element;
current_number%100==0;
num_elements++;
}
but steel dont work propertly.
|
|
|
|
|
How about:
while (...)
{
table[num_elements] = element;
num_elements++;
if ((num_elements % 100) == 0)
{
biggest(...);
smallest(...);
num_elements = 0;
}
}
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
|
|
|
|
|
I try, but compiler return erors:
too few arguments to function `float * biggest(float *, int)'
at this point in file
too few arguments to function `float * smallest(float *, int)'
at this point in file
|
|
|
|
|
I don't mean to sound mean but you should really be able to figure that one out.
-- Rocky Dean Pulley
|
|
|
|
|
O.K., Thank you anyway for yours great help and good understanding.
I'll try to do it well alone, but i'm not sure in access.
Thanks again and goodbay !
|
|
|
|
|
I want to switch dynamically from vertical to gorizontal split windows.
It's very easy (IMHO). But have little problem.
When my program started, I have two vertical split windows:
<br />
<br />
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT ,<br />
CCreateContext* pContext)<br />
{<br />
CreateSplitters(m_nRows, m_nCols, m_actRow, m_actCol, pContext);<br />
return TRUE;<br />
}<br />
<br />
BOOL CMainFrame::CreateSplitters(int nRows, int nCols, int m_actRow, int m_actCol, CCreateContext* pContext)<br />
{<br />
m_bInitSplitter = FALSE;<br />
my_RightView = RUNTIME_CLASS( CRightHTMLView );<br />
my_LeftView = RUNTIME_CLASS( CMyTree );<br />
<br />
<br />
<br />
CRect cr; <br />
GetClientRect( &cr);<br />
if ( !m_wndSplitter.CreateStatic( this, nRows, nCols ) ) <br />
{ <br />
MessageBox( "Error setting up splitter frames!", <br />
"Init Error!", MB_OK | MB_ICONERROR ); <br />
return FALSE; <br />
}<br />
int width = cr.Width();<br />
<br />
<br />
if ( !m_wndSplitter.CreateView( 0, 0, my_LeftView, <br />
CSize(width/5, cr.Height()), pContext ) || <br />
!m_wndSplitter.CreateView( m_actRow, m_actCol, my_RightView, <br />
CSize(4*width/5, cr.Height()), pContext )) <br />
{ <br />
MessageBox( "Error setting up splitter frames!", <br />
"Init Error!", MB_OK | MB_ICONERROR );<br />
return FALSE; <br />
}<br />
m_bInitSplitter = TRUE;<br />
<br />
return TRUE;<br />
<br />
}<br />
<br />
In in other functions I do not have pointer to CCreateContext structure, that is needed to create splitters.
Have can I retrive this pointer in any place of my object? This is very easy but I couldn't do it.
|
|
|
|
|
Hi all.
Has anyone any ideas on how to detect (programatically) the code page of a text file in Win32? At least in a way that makes the correct guess 90 % of the time?
For now, I'm testing different code pages with MultiByteToWideChar (CP_ANSI - CP_UTF8), counting the number of letters in the string (using iswalpha), and then the code page with the most letters wins. Feels shaky. This problem must have been around for ever, but I can't find any good ideas on how to deal with it.
Btw, there's a function called IsTextUnicode in the NLS API, does anyone know how to use it on reversed (MacOS) Unicode-files? I've tried the XXX_REVERSED flags, but the stupid function returns zero for every kind of input.
Any help or clues would be appreciated.
|
|
|
|
|
Recently I gone through a project in which I used a ".mdb" database.But whenever I took the application in different systems,I had to connect,once, the certain database manually by clicking "ODBC data sources Administrator" in control panel and choosing the right path.
I think it is not suitable for the users ,actually when the application is distributed,to do the right steps in connecting the database.
So,is it possible to code it within the project so that it automatically connects to its required database at the time of initialisation of the application for the first time?
|
|
|
|
|
joy007 wrote:
So,is it possible to code it within the project so that it automatically connects to its required database at the time of initialisation of the application for the first time?
Yes[^]
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
I have a list control in Icon view. I add somes items with icon into my list, and my list open, I don't see all the text for each items. What I see is the icon, and under the icon, the begin of the text with "...". How can I see the full text without I need to click on the icon ?
|
|
|
|
|