Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming

Reply
 
LinkBack Thread Tools
Old 3 Weeks Ago   #1 (permalink)
Halo > Half Life
 
Fatal05's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: San Luis Obispo, CA
Posts: 3,879

Rep: 321 Fatal05 is a proven memberFatal05 is a proven memberFatal05 is a proven memberFatal05 is a proven member
Unique Rep: 232
Hardware Reviews: 1
Trader Rating: 9
Default Functions with Loops in C

Well here I've created some code that prints a hollow rectangle, coarse and fine checkerboard. The only thing is, is that I need to create another function to reduce the number of loops in the entire program 9. I currently have 13 loops.

Needless too say, I'm stuck. I was wondering where I should put this function and could you give me an idea of what its structure should look like?

I believe I need to create a function that prints X's and blank spaces ("X", " ").

Halp.
Code:
#include <stdio.h>

void hollowRect(int width, int height){
   int row, col;

   printf("Hollow rectangle:\n");

   // First row
   for (col = 0; col < width; col++)
      printf("X");

   // Second row
   for (row = 0; row < height-2; row++){
      printf("\nX");
      for (col = 0; col < width-2; col++)
         printf(" ");
      printf("X");
   }

   // Last row
   printf("\n");
   for (col = 0; col < width; col++)
      printf("X");
}

void coarseCheckr(int width, int height){
    int row, col;

    printf("\n\nCoarse Checkerboard:\n");

   // Top Half

   for (row = 0; row < (height/2); row++){
   // Top Left
      for (col = 0; col < (width/2); col++){
         printf("X");
      }
   // Top Right
      for (col = (width/2); col < width; col++){
         printf(" ");
      }
      printf("\n");
   }

   // Bottom Half

   for (row = (height/2); row < height; row++){
   // Bottom Left
      for (col = 0; col < (width/2); col++){
         printf(" ");
      }
   // Bottom Right
      for (col = (width/2); col < width; col++){
         printf("X");
      }
      printf("\n");
   }
}

void fineCheckr(int width, int height){
   int row, col;

  printf("\nFine Checkerboard:\n");

   width += width%2;
   height += height%2;
   for (row = 0; row < height; row++){
      for (col = 0; row%2 == 0 && col < width; col+=2){
         printf("X ");
      }
      for (col = 0; row%2 == 1 && col < width; col+=2){
         printf(" X");
      }
      printf("\n");
   }
}
int main()
{
    int row, col, height, width;

    printf("Enter Height and Width:");
    scanf("%d%d", &height, &width);

    while (height < 2 || width < 2){
       printf("Please enter at least 2 for height and width: ");
       scanf("%d%d", &height, &width);
   }

   hollowRect (width, height);
   coarseCheckr (width, height);
   fineCheckr (width, height);
}
__________________
System: Peace and Quiet
CPU
E6300 B2 2.8ghz [1.33v]
Motherboard
DFI Blood Iron P35-T2RL
Memory
[2x2GB] OCZ SLI DDR2 800
Graphics Card
eVGA 8800GT SC 512MB
Hard Drive
Seagate 250GB 7200.10
Sound Card
Creative Audigy 4
Power Supply
FSP Blue Storm II 500W
Case
Antec Solo
CPU cooling
AC Freezer 7 Pro
GPU cooling
Stock
OS
Windows 7 7600 x64
Monitor
Westinghouse 19" [1680/1050]

Last edited by Fatal05 : 3 Weeks Ago at 09:46 PM
Fatal05 is offline Overclocked Account Fatal05's Gallery   Reply With Quote
Old 3 Weeks Ago   #2 (permalink)
4.0 GHz
 
dharmaBum's Avatar
 
intel nvidia

Join Date: Apr 2007
Location: Raleigh, NC
Posts: 747

Rep: 121 dharmaBum is acknowledged by manydharmaBum is acknowledged by many
Unique Rep: 89
Trader Rating: 0
Default

Reduce number of loops as in:
i) Make the code more efficient by reducing the number of executed loops
or
ii) Trim the number of code lines?

(ii) seems most likely so I'll do that, but if it's (i) I'll look more closely.

Quote:
I believe I need to create a function that prints X's and blank spaces ("X", " ").
You can do this, but there is a simpler way to limit the number of loops here.

You have:
Code:
   //first row
   for (col = 0; col < width; col++)
      printf("X");

   // Second row
   for (row = 0; row < height-2; row++){
      printf("\nX");
      for (col = 0; col < width-2; col++)
         printf(" ");
      printf("X");
   }

   // Last row
   printf("\n");
   for (col = 0; col < width; col++)
      printf("X");
Notice that here we loop over the same interval twice (the "top" and "bottom" lines), and for each row we basically repeat that interval with two less (the nested loop). This can be gathered up into one loop statement (the inner loop) with an "if/else" (or otherwise similar) check to see if we're "at the sides." (This can be done a few different and probably more efficient ways but I'm lazy and only give the clearest)

A simplification of the above code:
Code:
row=height;
col=width;
for(int i=0;i<row;i++)
{
   for(int j=0;j<col;j++)
   {
     //if we're at the "top" (i=0), "bottom" (i=row-1),
     //"left" (j=0) or "right" (j=col-1) then print an "X".
     if(i==0 || i==row-1 || j==0 || j==col-1) printf("X");
     //otherwise, print a " ".
     else printf(" ");
    }
    //We've taken care of this row.  Let's start a new one
    printf("\n");
}
//We've reduced four for loops to two!
This can be carried out with the same idea in the other sections of you're code. For example:

Code:
      for (col = 0; row%2 == 0 && col < width; col+=2){
         printf("X ");
      }
      for (col = 0; row%2 == 1 && col < width; col+=2){
         printf(" X");
      }
Can be made into one for loop, using a conditional statement inside the loop to check if the modulus is 1 or 2.


As I indicated at the start, this doesn't actually reduce the number of instructions sent to the CPU. This is a code-readability scheme, because it's easier to look at one loop that does several things than several loops that do one thing (although to the computer these are identical procedures.)

Edit: kept variable names consistent, syntax errors.
__________________
3DMark06: 19091 - 3DMark Vantage: P15264 - SuperPi: 10.968s

Programming Quote of the Day:
Bjarne Stroustrup:
Quote:
There are only two industries that refer to their customers as ‘users’.

System: Europa
CPU
E8500 4.36ghz @ 1.36v
Motherboard
EVGA 780i SLi P05 Bios
Memory
G.SKILL 4GB(2x2GB) @ 924MHz (5-4-4-12-2T)
Graphics Card
2xEVGA 8800GTS (G92) 512MB @800/2000/2110
Hard Drive
Seagate 500gbx2, (fake-)RAID0
Sound Card
Sound Blaster X-Fi XtremeGamer
Power Supply
CORSAIR 1000HX 1000W
Case
Gigabyte GZ-FA2CA-AJB Black Aluminum
CPU cooling
TDX 775 Block, 360 BlackIce rad
GPU cooling
MAZE5x2, TT copper HS
OS
Fedora10-86_64/Vista64
Monitor
22" Samsung SyncMaster 2232BW

Last edited by dharmaBum : 3 Weeks Ago at 10:54 PM
dharmaBum is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -5. The time now is 03:29 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License

Terms of Service / Forum Rules | Privacy Policy | DMCA Info | Advertising | Become an Official Vendor
Copyright © 2009 Shogun Interactive Development. Most rights reserved.
Page generated in 0.08600 seconds with 8 queries