You are here:

C/its a matrix display

Advertisement


Question
7 seg image_problem
7 seg image_problem  
Hi Tony,

I'm Sunil from India.I have been learning and using C from 3 Years, but recently I started using Dev C++ Version 2 Compiler for programming.

What I'm trying is to upload a syntax similar to 7 segment Display within a matrix by using Loop and if conditions. The program is below
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define ROW 6
#define COL 3
int disp_arr(int r,int c,char mat[6][3]);
int main()
{
    int i=0,j=0,r=0,c=0;
    char seg_disp[6][3];
    for(i=0;i<=ROW;i++)
    for(j=0;j<=COL;j++)
    seg_disp[i][j]=' ';
    for(i=0;i<=ROW;i++)
    {
         for(j=0;j<=COL;j++)
         {
         if(i==0||i==ROW/2||i==ROW)
         {
         if(j==COL||j==0)
         {
         seg_disp[i][j]=' ';
         printf(" ");
         }
         else
         {
         seg_disp[i][j]='*';
         printf("*");
         }
         }
         else
         {
         if(j==COL||j==0)
         {
         seg_disp[i][j]='*';
         printf("*");
         }
         else
         {
         seg_disp[i][j]=' ';
         printf(" ");
         }
         }
         }
         printf("\n");
    }
    printf("\n");
    disp_arr(ROW,COL,seg_disp);
    getch();
    return(0);
}          
int disp_arr(int r,int c,char mat[6][3])
{
    int i,j;
    for(i=0;i<=r;i++)
    {
         for(j=0;j<=c;j++)
         printf("%c",mat[i][j]);
         printf("\n");
    }
    return(0);
}

But the Output is as shown in Image attached.

Hope I'm clear with my Question, if not you can mail to me at the address mentioned ""

Answer
Hi Kuridi,

The problem is you are accessing out of bounds array indexes. "mat" is a 6x3 character array. The maximum indexes are 5 and 2. Your loops go from 0 to 6, and 0 to 3, which are outside the bounds of the array.

For example try using "<" instead of "<="

char seg_disp[ROW][COL];
   for(i=0;i<ROW;i++)
     for(j=0;j<COL;j++)
       seg_disp[i][j]=' ';

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tony Nazzal

Expertise

I will answer any c language programming and syntax questions.

Experience

I have almost a decade of c programming experience.

Education/Credentials
I have a bachelor of science degree and several years of industry experience.

©2016 About.com. All rights reserved.