Thread: making rows in terms of columns?

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    40

    making rows in terms of columns?

    Here is my code below. If I enter 2 for "How many rows question" and 9 for "How many values to be used question... I get the following result...

    Code:
    1     3      5     7     9
    2     4      6     8
    Now, the code does what I want but I'm asking how many rows the user wants.

    I want the user to be able to enter in how many columns he wants instead of how many rows rows (but I want the same result as before) how would I go about doing it?


    Code:
    #include <stdio.h>
    
    int func_col(int, int);
    
    int main()
    {
    	int q, a, b, c, x, row, max_value;
    
    	printf("How many rows? ");
    	scanf(" %d", &row);
    
    	printf("How many values to be used? ");
    	scanf(" %d", & max_value);
    		
    	q = func_col(max_value, row);
    	
    	for(b=1, c=0; c<row; c++)
    	{
    		x=b;
    		for(a=0; a<q+1; a++)
    		{
    			if(x<max_value+1)
    				printf("%d\t", x);
    			
    			x+=row;
    		}
    		b++;
    		printf("\n");
    	}
    }
    
    int func_col(int max_value, int row) 
    {
    	if(row<5)
    		return(max_value / row);
    	else
    		return((max_value / row)+1); 
    }

  2. #2
    Registered User AdamAksu's Avatar
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    1
    Something like below?
    I rewrote it and used other names for the variables.

    Code:
    #include <stdio.h>
    
    int calcRows( int cols, int maxVal );
    
    int main( )
    {
        int columns, rows, maxValue, startValue=1, incrementValue;
    
        printf( "How many columns? " );
        scanf( " %d", &columns );
    
        printf( "How many values to be used? " );
        scanf( " %d", & maxValue );
    
        rows = calcRows( columns, maxValue );
    
        for( int i=0; i<rows; i++ )
        {	
            incrementValue = startValue;
    
            for(int j=0; j<=columns; j++)
            {
                if( incrementValue <= maxValue )
                printf( "%d\t", incrementValue );
        		
                incrementValue += rows;
            }
            startValue++;
            printf( "\n" );
        }
    
        return 0;
    }
    
    int calcRows( int cols, int maxVal ){
        if( maxVal%cols == 0 )
            return 	maxVal/cols;
        else
            return 	maxVal/cols + 1;
    }

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    40
    sorry for late reply, but thanks! it works the way iwanted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "subscript requires array or pointer type"?
    By Nutka in forum C Programming
    Replies: 12
    Last Post: 12-06-2002, 05:51 PM
  2. calculation from rows and columns
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 07-08-2002, 07:44 PM
  3. Adding Columns and Rows..Help!
    By ProgrammingDlux in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 03:52 PM
  4. Blocking columns
    By y2jasontario in forum C Programming
    Replies: 2
    Last Post: 04-02-2002, 05:27 PM
  5. sizeof rows (not columns)
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 09-18-2001, 04:45 AM