Thread: maximum and minimum

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    17

    maximum and minimum

    I working with a program which have too plot a two-dimentionelt coordinates on the screen. I am almost finished except for one small problem which has caused me problems in a few days now. The fact is that the program should be able to receive a minimum and maximum value for both the x and y-axes. And then, the program have to first use the received maximum og minimum and then be able to calculate and write range values on the axes.

    This is my program so far:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char xText[] = "Tid(sek)";
    char yText[] = "Temp(C)";
    
    char a[16][45] =
    {
            {"                                            "},
            {"                                            "},
            {"              ^                              "},
            {"              |                              "},
            {"              |                              "},
            {"              |                              "},
            {"              |                              "},
            {"              |                              "},
            {"              |                              "},
            {"              |                              "},
            {"              |                              "},
            {"              |                              "},
            {"              |                              "},
            {"              +---|---|---|---|---|->        "},
            {"                                            "},
            {"                                            "}
    };
    
    void Putstr(int x, int y, const char* s)
    {
        int i;
        for(i = 0; s[i]; i++)
            a[y][x + i] = s[i];
    }
    void Putstr1(int x, int y, const char* s)
    {
        int i;
        for(i = 0; s[i]; i++)
            a[x][y + i] = s[i];
    }
    
    int main()
    {
        int l, i;
        double minx = 0.0;
        double maxx = 100.0;
        double miny = 0.0;
        double maxy = 100.0;
        int n;
    
        double max = 100.0;
        
        // x-axe Values:
        
        for(n = 0; n < 1; n++)
        {
            char s[3];
            sprintf(s, "%.1f", (max/5)*n);
            Putstr(13, 14, s);
        }
    
        for(n = 1; n < 2; n++)
        {
            char t[4];
            sprintf(t, "%.1f", (max/5)*n);
            Putstr(17, 15, t);
        }
        for(n = 2; n < 3; n++)
        {
            char u[4];
            sprintf(u, "%.1f", (max/5)*n);
            Putstr(21, 14, u);
        }
        for(n = 3; n < 4; n++)
        {
            char v[4];
            sprintf(v, "%.1f", (max/5)*n);
            Putstr(24, 15, v);
        }
        for(n = 4; n < 5; n++)
        {
            char b[4];
            sprintf(b, "%.1f", (max/5)*n);
            Putstr(29, 14, b);
        }
        for(n = 5; n < 6; n++)
        {
            char d[4];
            sprintf(d, "%.1f", (max/5)*n);
            Putstr(32, 15, d);
        }
        
        // y-axe values:
        
        for(n = 0; n < 1; n++)
        {
            char e[1];
            sprintf(e, "%.f", (max/5)*n);
            Putstr(11, 13, e);
        }
        for(n = 1; n < 2; n++)
        {
            char r[4];
            sprintf(r, "%.1f", (max/5)*n);
            Putstr(8, 11, r);
        }
        for(n = 2; n < 3; n++)
        {
            char g[4];
            sprintf(g, "%.1f", (max/5)*n);
            Putstr(8, 9, g);
        }
        for(n = 3; n < 4; n++)
        {
            char f[4];
            sprintf(f, "%.1f", (max/5)*n);
            Putstr(8, 7, f);
        }
        for(n = 4; n < 5; n++)
        {
            char h[4];
            sprintf(h, "%.1f", (max/5)*n);
            Putstr(8, 5, h);
        }
        for(n = 5; n < 6; n++)
        {
            char m[4];
            sprintf(m, "%.1f", (max/5)*n);
            Putstr(7, 3, m);
        }
    
        printf("Minimums og maksimums værdi på x-aksen: ");
        fflush(stdout);
        scanf("%lf, %lf", &minx, &maxx);
        printf("Minimums og maksimums værdi på y-aksen: ");
        fflush(stdout);
        scanf("%lf, %lf", &miny, &maxy);
    
        memcpy(a[1] + 10, yText, strlen(yText));
        memcpy(a[13] + 45 - strlen(xText), xText, strlen(xText));
    
        for(l = 0; l < 16; l++,printf("\n"))for(i=0; i<45; i++)
        {
            printf("%c", a[l][i]);
        }
    
        return 0;
    }

  2. #2
    Banned
    Join Date
    Dec 2008
    Posts
    49
    So simply put, you are trying to graph these, correct? Why just make an array of chars and graph within that array, then output the array to the screen.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    17
    No thats not my problem. The problem is that I do not know how to make a simple code that can receive a maximum and minimum value for both the x and y-axes, and the be able to calculate and write range values on the axes. do you understand what I mean??? R

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    17
    Right now I can make the values for both x and y-axes to be graphed, but my program does not care for the maximum and minimum values I enter.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well inputting the values before drawing the axes would be good.

    Then max = maxx - minx would be the range, which you divide up into 5.

    > for(n = 5; n < 6; n++)
    Why not just n = 5 and be done with it?
    These 5 loops are begging to be a function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Along with what Salem mentioned, why not make nexted loops?

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    17
    The reason why I have 6 loops is because I want them to be displayed on the screen like this:

    Code:
    Minimums og maksimums værdi på x-aksen: 0.0, 50.0
    Minimums og maksimums værdi på y-aksen: 0.0, 50.0
                                                 
              Temp(C)                            
                  ^                              
           100.0  |                              
                  |                              
            80.0  |                              
                  |                              
            60.0  |                              
                  |                              
            40.0  |                              
                  |                              
            20.0  |                              
                  |                              
               0  +---|---|---|---|---|->Tid(sek)
                 0.0     40.0    80.0            
                     20.0   60.0    100.0
    Right now when I run my program shows the following:
    Code:
    Minimums og maksimums værdi på x-aksen: 0.0, 50.0
    Minimums og maksimums værdi på y-aksen: 0.0, 50.0
                                                 
              Temp(C)                            
                  ^                              
           50.0   |                              
                  |                              
            40.0  |                              
                  |                              
            30.0  |                              
                  |                              
            20.0  |                              
                  |                              
            10.0  |                              
                  |                              
               0  +---|---|---|---|---|->Tid(sek)
                 0.0     20.0    40.0            
                     10.0   30.0    50.0
    But the problem is that my code does not take care of what I write the maximum and minimum values.

  8. #8
    Banned
    Join Date
    Dec 2008
    Posts
    49
    How is that incorrect? It looks great You could nest the loop to do the same thing though, was merely my point. Wait... on second thought, your loops are kind of funky. They shouldn't even be loops at all.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like I said, you layout the numbers before you read in the new min/max.

    Also, the fact that you put the x axis on 2 rows is not an obstacle to using a function.
    It's only &#37;2 to decide whether it's the upper or lower row.

    Also, e[1] is a bug.
    As are all your other very short arrays, which fail to account for the \0.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    17
    My code is incorrect beacuse the program does not take care of what I write the maximum an minimum values. When the program ask me to write a the maximum and minimum for x-axes, and maximum for y-axes, and I write 0.0, 100: (I get this output):

    Code:
    Minimums og maksimums værdi på x-aksen: 0.0, 100.0
    Minimums og maksimums værdi på y-aksen: 0.0, 100.0
                                                 
              Temp(C)                            
                  ^                              
           50.0   |                              
                  |                              
            40.0  |                              
                  |                              
            30.0  |                              
                  |                              
            20.0  |                              
                  |                              
            10.0  |                              
                  |                              
               0  +---|---|---|---|---|->Tid(sek)
                 0.0     20.0    40.0            
                     10.0   30.0    50.0
    And thats not true beacuse I wrote the maximum and minimum on both axes to be 0.0, 100.0.

    Can you please show me a way to make my program understand how to use the maximum oan d minimum value the user write. And not use what I wrote in my code: double maxx=50;
    my code is now:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char xText[] = "Tid(sek)";
    char yText[] = "Temp(C)";
    
    char a[16][45] =
    {
    		{"                                             "},
    		{"                                             "},
    		{"              ^                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              +---|---|---|---|---|->        "},
    		{"                                             "},
    		{"                                             "}
    };
    
    void Putstr(int x, int y, const char* s)
    {
    	int i;
    	for(i = 0; s[i]; i++)
    		a[y][x + i] = s[i];
    }
    void Putstr1(int x, int y, const char* s)
    {
    	int i;
    	for(i = 0; s[i]; i++)
    		a[x][y + i] = s[i];
    }
    
    int main()
    {
    	int l, i;
    	double minx;
    	double maxx;
    	double miny;
    	double maxy;
    	int n;
    
    	printf("Minimums og maksimums værdi på x-aksen: ");
    	fflush(stdout);
    	scanf("%lf, %lf", &minx, &maxx);
    	printf("Minimums og maksimums værdi på y-aksen: ");
    	fflush(stdout);
    	scanf("%lf, %lf", &miny, &maxy);
    	
    	double max = 50.0;
    
    	// x-aske intervalværdier:
    
    	for(n = 0; n < 1; n++)
    	{
    		char s[32];
    		sprintf(s, "%.1f", (max/5)*n);
    		Putstr(13, 14, s);
    	}
    
    	for(n = 1; n < 2; n++)
    	{
    		char t[32];
    		sprintf(t, "%.1f", (max/5)*n);
    		Putstr(17, 15, t);
    	}
    	for(n = 2; n < 3; n++)
    	{
    		char u[32];
    		sprintf(u, "%.1f", (max/5)*n);
    		Putstr(21, 14, u);
    	}
    	for(n = 3; n < 4; n++)
    	{
    		char v[32];
    		sprintf(v, "%.1f", (max/5)*n);
    		Putstr(24, 15, v);
    	}
    	for(n = 4; n < 5; n++)
    	{
    		char b[32];
    		sprintf(b, "%.1f", (max/5)*n);
    		Putstr(29, 14, b);
    	}
    	for(n = 5; n < 6; n++)
    	{
    		char d[32];
    		sprintf(d, "%.1f", (max/5)*n);
    		Putstr(32, 15, d);
    	}
    
    	// y-aske intervalværdier:
    
    	for(n = 0; n < 1; n++)
    	{
    		char e[32];
    		sprintf(e, "%.f", (max/5)*n);
    		Putstr(11, 13, e);
    	}
    	for(n = 1; n < 2; n++)
    	{
    		char r[32];
    		sprintf(r, "%.1f", (max/5)*n);
    		Putstr(8, 11, r);
    	}
    	for(n = 2; n < 3; n++)
    	{
    		char g[32];
    		sprintf(g, "%.1f", (max/5)*n);
    		Putstr(8, 9, g);
    	}
    	for(n = 3; n < 4; n++)
    	{
    		char f[32];
    		sprintf(f, "%.1f", (max/5)*n);
    		Putstr(8, 7, f);
    	}
    	for(n = 4; n < 5; n++)
    	{
    		char h[32];
    		sprintf(h, "%.1f", (max/5)*n);
    		Putstr(8, 5, h);
    	}
    	for(n = 5; n < 6; n++)
    	{
    		char m[32];
    		sprintf(m, "%.1f", (max/5)*n);
    		Putstr(7, 3, m);
    	}
    
    	memcpy(a[1] + 10, yText, strlen(yText));
    	memcpy(a[13] + 45 - strlen(xText), xText, strlen(xText));
    
    	for(l = 0; l < 16; l++,printf("\n"))for(i=0; i<45; i++)
    	{
    		printf("%c", a[l][i]);
    	}
    
    	return 0;
    }
    Sorry for all my confusion, but I have for about 1 months ago learned to program in C, and I find it really really hard!

  11. #11
    Banned
    Join Date
    Dec 2008
    Posts
    49
    Quote Originally Posted by aslak View Post
    My code is incorrect beacuse the program does not take care of what I write the maximum an minimum values. When the program ask me to write a the maximum and minimum for x-axes, and maximum for y-axes, and I write 0.0, 100: (I get this output):

    Code:
    Minimums og maksimums værdi på x-aksen: 0.0, 100.0
    Minimums og maksimums værdi på y-aksen: 0.0, 100.0
                                                 
              Temp(C)                            
                  ^                              
           50.0   |                              
                  |                              
            40.0  |                              
                  |                              
            30.0  |                              
                  |                              
            20.0  |                              
                  |                              
            10.0  |                              
                  |                              
               0  +---|---|---|---|---|->Tid(sek)
                 0.0     20.0    40.0            
                     10.0   30.0    50.0
    And thats not true beacuse I wrote the maximum and minimum on both axes to be 0.0, 100.0.

    Can you please show me a way to make my program understand how to use the maximum oan d minimum value the user write. And not use what I wrote in my code: double maxx=50;
    my code is now:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char xText[] = "Tid(sek)";
    char yText[] = "Temp(C)";
    
    char a[16][45] =
    {
    		{"                                             "},
    		{"                                             "},
    		{"              ^                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              |                              "},
    		{"              +---|---|---|---|---|->        "},
    		{"                                             "},
    		{"                                             "}
    };
    
    void Putstr(int x, int y, const char* s)
    {
    	int i;
    	for(i = 0; s[i]; i++)
    		a[y][x + i] = s[i];
    }
    void Putstr1(int x, int y, const char* s)
    {
    	int i;
    	for(i = 0; s[i]; i++)
    		a[x][y + i] = s[i];
    }
    
    int main()
    {
    	int l, i;
    	double minx;
    	double maxx;
    	double miny;
    	double maxy;
    	int n;
    
    	printf("Minimums og maksimums værdi på x-aksen: ");
    	fflush(stdout);
    	scanf("%lf, %lf", &minx, &maxx);
    	printf("Minimums og maksimums værdi på y-aksen: ");
    	fflush(stdout);
    	scanf("%lf, %lf", &miny, &maxy);
    	
    	double max = 50.0;
    
    	// x-aske intervalværdier:
    
    	for(n = 0; n < 1; n++)
    	{
    		char s[32];
    		sprintf(s, "%.1f", (max/5)*n);
    		Putstr(13, 14, s);
    	}
    
    	for(n = 1; n < 2; n++)
    	{
    		char t[32];
    		sprintf(t, "%.1f", (max/5)*n);
    		Putstr(17, 15, t);
    	}
    	for(n = 2; n < 3; n++)
    	{
    		char u[32];
    		sprintf(u, "%.1f", (max/5)*n);
    		Putstr(21, 14, u);
    	}
    	for(n = 3; n < 4; n++)
    	{
    		char v[32];
    		sprintf(v, "%.1f", (max/5)*n);
    		Putstr(24, 15, v);
    	}
    	for(n = 4; n < 5; n++)
    	{
    		char b[32];
    		sprintf(b, "%.1f", (max/5)*n);
    		Putstr(29, 14, b);
    	}
    	for(n = 5; n < 6; n++)
    	{
    		char d[32];
    		sprintf(d, "%.1f", (max/5)*n);
    		Putstr(32, 15, d);
    	}
    
    	// y-aske intervalværdier:
    
    	for(n = 0; n < 1; n++)
    	{
    		char e[32];
    		sprintf(e, "%.f", (max/5)*n);
    		Putstr(11, 13, e);
    	}
    	for(n = 1; n < 2; n++)
    	{
    		char r[32];
    		sprintf(r, "%.1f", (max/5)*n);
    		Putstr(8, 11, r);
    	}
    	for(n = 2; n < 3; n++)
    	{
    		char g[32];
    		sprintf(g, "%.1f", (max/5)*n);
    		Putstr(8, 9, g);
    	}
    	for(n = 3; n < 4; n++)
    	{
    		char f[32];
    		sprintf(f, "%.1f", (max/5)*n);
    		Putstr(8, 7, f);
    	}
    	for(n = 4; n < 5; n++)
    	{
    		char h[32];
    		sprintf(h, "%.1f", (max/5)*n);
    		Putstr(8, 5, h);
    	}
    	for(n = 5; n < 6; n++)
    	{
    		char m[32];
    		sprintf(m, "%.1f", (max/5)*n);
    		Putstr(7, 3, m);
    	}
    
    	memcpy(a[1] + 10, yText, strlen(yText));
    	memcpy(a[13] + 45 - strlen(xText), xText, strlen(xText));
    
    	for(l = 0; l < 16; l++,printf("\n"))for(i=0; i<45; i++)
    	{
    		printf("%c", a[l][i]);
    	}
    
    	return 0;
    }
    Sorry for all my confusion, but I have for about 1 months ago learned to program in C, and I find it really really hard!
    You are telling it to do something that you probably are not meaning to.

  12. #12
    Registered User
    Join Date
    Dec 2008
    Posts
    17
    yes that maybe sounds cracy, it is because I could not figure out how to get the program to get make the axes to the value for max.and min. I wrote to start with. But now I am totally lost how to combine the max. and min. value I wite to the 6 loops.

  13. #13
    Banned
    Join Date
    Dec 2008
    Posts
    49
    My point is you always print off the hard coded max value. Plus you always start from 0, which completely ignores the whole idea of a minimum.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    /me points to post #5
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  15. #15
    Banned
    Join Date
    Dec 2008
    Posts
    49
    I don't want to rewrite your function. I know you are sooo close to doing it on your own. I can honestly tell you the thrill of self-enlightenment is far greater than the satisfaction of hand-me-down answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-28-2009, 09:58 PM
  2. Displaying Minimum and Maximum values from input
    By mgardnertech in forum C Programming
    Replies: 1
    Last Post: 06-29-2008, 08:47 PM
  3. Maximum and Minimum Values
    By swaugh in forum C Programming
    Replies: 7
    Last Post: 12-16-2006, 09:43 PM
  4. Maximum And Minimum
    By drdodirty2002 in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2004, 12:39 AM
  5. Replies: 2
    Last Post: 10-31-2002, 07:27 PM