开发者

finding max in bunch of arrays in C

开发者 https://www.devze.com 2023-02-15 11:41 出处:网络
I have couple of questions. I have a text file which contains these information x0,x1,y0,y1 142,310,0,959

I have couple of questions. I have a text file which contains these information

x0,x1,y0,y1
142,310,0,959
299,467,0,959
456,639,0,959
628,796,0,959
  1. First, I want to read the text file using fscanf and get only the numbers into 4 arrays, c1, c2, c3, and c4 by skipping the first line. So the final result will be

    c[1] = {142, 310, 0, 959}
    c[2] = {299, 467, 0, 959}
    c[3] = {456, 639, 0, 959}
    c[4] = {628, 796, 0, 959}
    
  2. Then, for each c[1] to c[4], I want to find the maximum integer and store it in [x, y] datatype. So for example in c[1], the max will be max[1] = [310, 959].

Ca开发者_运维问答n anyone help? Other C solution other than using arrays to solve this problem is welcome as well.

In matlab, the code is

fid = fopen('foo.txt','r');
c = textscan(fid,'%d%d%d%d','delimiter',',','headerlines',1);
fclose(fid);

This will simply ignore the first line, then copy rest of numbers into arrays in matlab. I would like to translate this code into C. Thank you very much.


Although not satsifying your question directly, I have provided an example of reading points using a struct, std::istream and std::vector. These are preferred over fscanf and arrays.

struct Point
{
  unsigned int x;
  unsigned int y;

  friend std::istream& operator>>(std::istream& inp, Point& p);
};

std::istream& operator>>(std::istream& inp, Point& p)
{
  inp >> p.x;
  //Insert code here to read the separator character(s)
  inp >> p.y;
  return inp;
}

void Read_Points(std::istream& input, std::vector<Point>& container)
{
  // Ignore the first line.
  inp.ignore(1024, '\n');

  // Read in the points
  Point p;
  while (inp >> p)
  {
     container.push_back(p);
  }
  return;
}

The Point structure provides more readability, and IMHO, more versatility because you can declare other classes using a Point:

class Line
{
  Point start;
  Point end;
};

class Rectangle
{
  Point upper_left_corner;
  Point lower_right_corner;
  friend std::istream& operator>>(std::istream& inp, Rectangle& r);
};

You can add methods for reading from a file using the operator>> for point:

std::istream& operator>> (std::istream& input, Rectangle& r)
{
  inp >> r.upper_left_corner;
  //Insert code here to read the separator character(s)
  inp >> r.lower_left_corner;
  return inp;
}

Arrays are a problem and can lead to nasty runtime errors such as buffer overruns. Perfer std::vector, classes or structures to arrays.

Also, since std::istream is used, these structures and classes can be easily used with std::cin and files (std::ifstream):

  // Input from console
  Rectangle r;
  std::cin >> r;


Just in case your

Other format to solve this problem is welcome as well.

also means other language, Python code to solve this problem will be like this

fo = open('a.txt','r')
line = fo.readline() #ignore first line
max = [sorted(map(int,line.split(',')))[-2:] for line in fo]

and the result will be

[[310, 959], [467, 959], [639, 959], [796, 959]]

0

精彩评论

暂无评论...
验证码 换一张
取 消