It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to write a simple program that reads integers from a data file and outputs the minimum and maximum value. The first integer of the input file will indicate how many more integers will be read, and then the integers will be listed.
Example input file:
5 100 -25 42235 7 -1
As simple as this is, i'm not sure where to start. I imagine I will need to create an array of size = the first integer, then increment my position in the array and compare to a min/max value.
I am not sure what the syntax would be to declare an array with the first integer, then fill the array with the remaining integers, or if that is even the right approach. After creating the array, I should have no problem assigning values to min/max variables with a loop increasing position through the array.
You don't need an array. Just keep track of the current minimum and maximum (they start as the first number you read). After you read each number, if it's lower than the minimum it becomes the new minimum, and if it's higher than the maximum it becomes the maximum.
There is no need to use an array to store data. You just need to find out minimum and maximum values from data received from a file.
long lMin = 0, lMax = 0;
int nCount;
long lNo;
// open file
fscanf( file, "%d", &nCount );
for( int i = 0; i < nCount; i++ )
{
fscanf( file, "%ld", &lNo );
if( lNo > lMax )
lMax = lNo;
if( lNo < lMin )
lMin = lNo;
}
printf(" Min = %ld Max = %ld\n", lMin, lMax );
// close file
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have a Structure, in the StructureI have an Array,
I read a text file and then open it in `Array Into Structure ',
What I have is a list of names, Last, results.
so what is the best way to find a row number and select which row to delete and delete it? I said, Array into a Structure.?
I know I can use memmove and realloc but how do I use these?
Well, all you can do is move the following elements towards the start, and decrease the "logical" length. The logical length is different from the physical length, which is the maximum number of elements the array can hold, based on how much memory has been allocated.
So, assuming an array starting at array and with count elements, code to delete the n:th element would be:
if( n < count - 1)
memmove(array + n, array + n + 1, ((count - n) - 1) * sizeof *array);
--count;
This copies the following elements (unless you're deleting the very last one, in which case there's nothing to copy) and then decreases the logical length.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Given a shifted array (for example):
[17 34 190 1 4]
which shifted from (we don't know original)
[1 4 17 34 190]
What would be a good function to find the position of that number?
For example if I pass 1, it would return 3th position.
linear search for answer would always work, but I believe you can get there in O(log) time.
Some sort of binary search for the shift point via checking if the value of the shift sorted array goes against what it is supposed it. Like creating a trie. Keep forming the sorted tree until you find the "illegal" node (man this is glossing over a lot of details - I know). That tells you where the inflection point is and you now treat the array as 2 sorted vectors. Quickly check to see if the value to find is larger than the max entry of each so we know which vector to search. BSearch the sorted vector for your value and return its index.
The hard part is finding the inflection point. :)
You would have to scan the array.
size_t pos_in_arr(int *arr, size_t arr_size, int match)
{
size_t i;
for (i = 0; i < arr_size; i++)
if (arr[i] == match)
break;
return i;
}
This function would return the position as asked, or one more than the maximum position in case the element is not found.
The solution is what you ask, but it is probably not what you need, because it does not use in any way the fact that the array has been shifted. I suspect the original problem to be more complex.
For example if you knew that in the original array one element was fifth and now is seventh, and the element you are looking for was twenty-third, you could answer "twenty-fifth" without actually scanning the array up to the twenty-fifth position, which could be the point of the whole exercise. But to build such a solution, one would need to know more about the problem.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I would like to get the distinct count of the column of a large data file using C.How can I do it.Please kindly advise me.Thanks.My sample data file is as below.
For 2nd attribute the distinct count is 6.
399547,v4149,p3178,1990,2065,fraud
399940,v5852,p3194,8278,2180,fraud
399983,v3476,p3199,766,1125,fraud
400206,v3467,p3216,494,311000,fraud
400345,v4497,p3219,1211,432100,fraud
400471,v3473,p3225,41392,3710,fraud
400498,v3476,p3225,102,23820,fraud
401325,v4497,p3297,1322,1110,fraud
Make a search tree for every column. Let's say you have 10 rows in a file with 2 distinct values for the nth column viz. 3456 and 3457. Your search tree for nth column will look like:
You'll end up with 6 Search trees. Once you have read the entire file, traverse all possible paths in each search tree and that will give you the number of distinct values.
Read and split every line.
Put the second attributes into an array.
qsort the array
You have now an array with equal strings adjacent to each other. You can loop over the array and count different entries.
If your entries are all 5 characters long, otherwise you must malloc() memory for each attribute.
char (*array)[6];
int i;
int n; /* number of lines read */
int distinct = 1;
/* read the data file and put it into array */
/* qsort() array */
for (i = 1; i < n; ++i) {
if (strcmp(array[i], array[i - 1]) != 0)
++distinct;
}
printf("There are %d distinct rows\n", distinct);
You can use std::map<std::string,int> - it will hold key-value pairs, where key is vNNNN, and value is number of repetitions.
First loop will scan input file and populate this map, then number of keys in map will be distinct count.
EDIT: If you cannot use C++ and do require C, you will have to find some hashmap library for C, like sparsehash.
If amount of data is really, really big, it is possible that it will not fit in memory. In this case, I would recommend to use SQLite temporary database to parse, store and index your data and then use standard SELECT DISTINCT on it.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to convert this string "09195462965" to an int but I'm running into problems.
snippet of my code:
int n, p, pnum=0;
char buffer[256];
char *endptr;
long pnumber;
bzero(buffer,256);
p = read(sock,buffer,255);
pnumber = strtol(buffer, &endptr, pnum);
printf("n: %ld",pnumber);
p = write(sock,buffer,sizeof(buffer));
A client sends a string of "09195462965" then the server receives it.
Now on the server that string must be turned into an int i.e. 09195462965.
Note: the server sends the number as string.
You're using strtol() incorrectly, the last parameter should be the base that you want. For example if you want to store that number in base 10 (decimal):
long pnumber;
pnumber = strtol("09195462965", NULL, 10); //nst char *nptr is "09195462965"
//char **endptr is null
//int base is 10 for decimal
printf("n: %ld",pnumber);
>> 9195462965
Make sure you read the man page for the function you're using.
Passing pnum (which is set to 0) as your doing for the last parameter is causing it to spit back "0", because of the number you're passing in.
09195462965 has digits from 0-9 (so I assume you wanted dec) if you pass in "0" to strtol() then it's going to see that first 0 and will treat the number has octal, the problem with that is that octal numbers go from 0-7, thus the 9's are "out of bounds" for an octal number and as such strtol() spits back 0.
with a number like: 07175462765, you'd be fine to pass in pnum when it's 0.
That's too big to fit in an int. Try strtoumax and store it in an uintmax_t.
uintmax_t pnumber = strtoumax(buffer, &endptr, 10);
if (pnumber == UINTMAX_MAX && errno == ERANGE)
/* Too big. */
Alternatively, if you don't have strtoumax you can try strtoull et al.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Does anyone know how to change the code below from enthusiasticgeek to allow for user input (e.g. Input n, and then n integers)? Output these n integers and the median. n is odd and positive and is less than 1 million.
Code pasted from enthusiasticgeek.com:
#include <stdio.h>
#include <stdlib.h>
#define ELEMENTS 6
int values[] = { 40, 10, 100, 90, 20, 25 };
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
int main ()
{
int n;
qsort (values, ELEMENTS, sizeof(int), compare);
for (n=0; n<ELEMENTS; n++)
{ printf ("%d ",values[n]); }
printf ("median=%d ",values[ELEMENTS/2]);
return 0;
}
use:
int values[static or dynamic allocation dependent]
for (i=0;i<n;i++){
printf("Enter number:");
scanf(%d, value[i]);
}
instead of just hardcoding the values for the values array.
Make sure you either dynamically declare the array, or statically make it large enough to accept the number of integers you want, 1,000,000 but thats a ton of memory. No one's going to take that much time to enter that many numbers manually. Probably safe with declaring it at 30 tops.
Or if you're planning to use the "define elements" part, you only need to declare the values array to be 6.