|
![]() |
Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming | |
Functions with Loops in C
|
||
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||||
|
Halo > Half Life
![]() |
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);
}
Last edited by Fatal05 : 3 Weeks Ago at 09:46 PM |
|||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||||
|
4.0 GHz
![]() |
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:
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");
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!
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");
}
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:
Last edited by dharmaBum : 3 Weeks Ago at 10:54 PM |
|||||||||||||||
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|