Thread: Need help making a program in C

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    1

    Need help making a program in C

    hey all, I've made this function that makes a thing like this:


    Imgur: The most awesome images on the Internet

    So far, I have all the code done, I just can't get the function to generate this in a ppm file to work. The program compiles and everything, it just gives weird ppm files as a result. Here's my code:

    Code:
    #include <stdio.h>
    
    void make_header (int width, int height);
    void print_Row (int yValue, int width, int height);
    void make_pixel (unsigned char r, unsigned char g, unsigned char b);
    
    void make_header (int width, int height)
    {
        fprintf(stdout,"P6\n");
        fprintf(stdout,"%d %d 255\n",width, height);
    }
    void print_Row (int yValue, int width, int height){
    	int x;
    	float slope1 = height / width, slope2 = -1*slope1;
    
    	for(yValue = 0; yValue < height; yValue++){
    		for(x = 0; x < width; x++){
    			if(yValue < slope1 * x && yValue < slope2 * x){
    				make_pixel(255, 0, 0);}//Makes red
    			else if(yValue<slope1 * x && yValue >= slope2 * x){
    				make_pixel(0, 255, 0);}//Makes green
    			else if(yValue>= slope1 * x && yValue>=slope2 * x){
    				make_pixel(0, 0, 255);}//Makes blue
    			else(yValue >= slope1 * x && yValue < slope2 * x);{
    				make_pixel(255, 255, 0);}//Makes yellow
    			}
    		}
    	}
    void make_pixel (unsigned char r, unsigned char g, unsigned char b)
    {
    	fprintf(stdout,"%c%c%c", r,g,b);
    }
    int main()
    {
        	float slope1, slope2, wmid, hmid, yValue ;
       	int width, height;
        
        	fprintf(stderr, "\nEnter a value for width: ");
        	fscanf(stdin, "%d", &width);
        	fprintf(stderr, "\nEnter a value for height: ");
        	fscanf(stdin, "%d", &height);
        	make_header (width, height);
    	slope1 = height / width;
        	slope2 = -1*slope1;
        	
    	print_Row (yValue, width, height);
    return(0);
    }
    Basically what I need it to do is to divide a rectangle of dimensions entered by the user into 4 quadrants (divided diagonally from the corners). I'm trying to do this by dividing the rectangle by two slope functions and then trying to see if a certain point is above both, above one but below another, or below both and it determines which color to print.

    I can't seem to get it to do this, can somebody help me please?
    Last edited by Salem; 10-09-2015 at 11:16 PM. Reason: inlined the code!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well how about fixing the fact you're passing garbage to your function.
    Code:
    $ gcc -Wall bar.c
    bar.c: In function ‘print_Row’:
    bar.c:24:11: warning: statement with no effect [-Wunused-value]
    bar.c: In function ‘main’:
    bar.c:35:35: warning: unused variable ‘hmid’ [-Wunused-variable]
    bar.c:35:29: warning: unused variable ‘wmid’ [-Wunused-variable]
    bar.c:35:21: warning: variable ‘slope2’ set but not used [-Wunused-but-set-variable]
    bar.c:46:13: warning: ‘yValue’ is used uninitialized in this function [-Wuninitialized]
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help making a program
    By XtC10 in forum C++ Programming
    Replies: 9
    Last Post: 08-13-2010, 09:41 AM
  2. Making a program that makes a program?
    By C-isCool in forum C Programming
    Replies: 3
    Last Post: 07-06-2007, 07:12 PM
  3. making a program leave a msg for background program when it closes
    By superflygizmo in forum Windows Programming
    Replies: 2
    Last Post: 02-06-2006, 07:44 PM
  4. I need help with making a program which....
    By Tonyukuk in forum C# Programming
    Replies: 1
    Last Post: 04-16-2003, 10:49 PM
  5. help making a program run
    By Dummies102 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2002, 12:51 PM

Tags for this Thread