line transit in array

This is a discussion on line transit in array within the C Programming forums, part of the General Programming Boards category; Hi, I need to take an array & an integer from stdin. the integer will set how much will a ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    line shifting in array

    Hi,
    I need to take an array & an integer from stdin. the integer will set how much will a lines in array move towards upwards.
    example:

    input:

    line 0 1 12 5
    line 1 7 5 3
    line 2 9 7 2

    integer=1

    line 0 7 5 3
    line 1 9 7 2
    line 2 1 12 5

    this is what i wrote. it doesnt work very well...

    Code:
    #include<stdio.h>
    #define M 20
    
    int A[M][M]={0},i,j,m,x;
    void main()
    {
    puts ("enter the dimension of array A");
    scanf ("%d", &x);
    printf ("\nenter %d positive integers for array A\n", x*x);
    for (i=0;i<x;i++)
            {
            for (j=0;j<x;j++)
    
            scanf ("%d", &A[i][j]);
            }
    printf ("how much lines you want to move in the array? no. should be smaller then %d\n",x);
    scanf ("%d", &m);
    if (m>x||m<0)
    	puts ("no. to big");
    
    else
    	{
    	for (j=1;j<=x;j=j-m)
    		for (i=0;i<x;i++)
    
    			printf ("%d", A[i][j]);
            printf ("\n");
    	}
      }
    what do you think?

    Ronen
    Last edited by ronenk; 06-23-2004 at 10:32 PM.

  2. #2
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    What about constrcting an array with undefined size??
    For each line do a gets(), and read all numbers in it. End a line with enter.
    If the user writes an empty string the cicle ends, then do the shift stuff.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <ctype.h>
    
    int main(){
    	int shift;
    	int array[100][100], bckp[100];
    	int xlen=100, ylen=0, xline=0, yline=0, i, j, k;
    	char line[500];
    	
    //the newline is also read, therefore buffer flushing isn't necessary	
    	scanf("%d\n", &shift);
    
    	if(shift<1) return 0;
    	
    	while(1){
    		fgets(line, sizeof(line), stdin);
    		if(!line[0] || line[0]=='\n') break;
    		n=strchr(line, '\n');
    		if(n) *n=0;
    
    		for(i=0;line[i];){
    			while(line[i]==' ') i++;//skip spaces
    			if(line[i]=='\n')//end of line				
    				break;
    			array[ylen][xline++]=atoi(line+i);
    			while(isdigit(line[i])) i++;
    		}
    		if(xline<xlen) xlen=xline;
    		xline=0;
    		ylen++;
    	}
    	shift%=ylen;//prevent excess of cicles
    
    //------- shifting the numbers
    	while(shift-->0){
    		for(i=0;i<xlen;i++) bckp[i]=array[0][i];
    
    		for(j=0;j<ylen-1;j++){
    			for(i=0;i<xlen;i++)
    				array[j][i]=array[j+1][i];
    		}
    		for(i=0;i<xlen;i++)
    			array[ylen-1][i]=bckp[i];
    	}
    //--------- presenting the result
    
    	for(j=0;j<ylen;j++){
    		for(i=0;i<xlen;i++)
    			printf("%d\t", array[j][i]);
    		puts("");
    	}
    	getch();
    	return 0;
    }
    I hope if you don't mind that I post the already solved solution here... with the input stuff I sugested.
    Last edited by xErath; 06-23-2004 at 08:03 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by xErath
    What about constrcting an array with undefined size??
    For each line do a gets(), and read all numbers in it.
    ...
    Code:
    ...
    	fflush(stdin);//clear the buffer
    ...
    		gets(line);
    I hope if you don't mind that I post the already solved solution here... with the input stuff I sugested.
    You're just full of horrible ideas today, aren't you? Read the FAQs on both.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Hum, i knew that stuff with the buffer overflow with gets... In fact that happens to evry function that receives pointers wihout enough memory allocated. Plus VC++ libraries, which is what I use, don't have the strtok() function.
    About fflush(stdin) I wasn't aware of it, although I've always doubted of it, like, sending data back to the keyboard isn't a very good thing...

    So fflush(stdin) becomes
    while(kbhit()) getch();

    //EDIT
    while(kbhit()) getch(); isn't working...
    Bah.
    Last edited by xErath; 06-23-2004 at 08:01 PM.

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Look in the faq for how to flush input buffer:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >In fact that happens to evry function that receives pointers wihout enough memory allocated.
    No, it happens with every function that doesn't know when to stop.

    >Plus VC++ libraries, which is what I use, don't have the strtok() function.
    VC++ supports the C standard library for as far back as I can remember. strtok is in <string.h>, and is a standard function so you can be guaranteed that it's there or you're free to lawsuit. Actually, I'm just kidding about the lawsuit.

    >sending data back to the keyboard isn't a very good thing...
    Well, the definition of a flush is writing unwritten data. That's fundamentally different from what people expect from fflush(stdin). They expect to read from the input buffer and discard. Both concepts empty the buffer, but because the two operations are totally different, one can't expect fflush(stdin) to do both. That's the logical explanation. The practical explanation is that the standard requires an output stream, so calling fflush on an input stream is undefined.

    >So fflush(stdin) becomes
    >while(kbhit()) getch();
    From undefined to nonportable and broken. Better, but not much. The first thing you should realize is that if you need to flush the input stream, your design is already flawed. But if you really need to you can do it portably. Because everyone basically wants to terminate the line, we usually suggest testing for EOF and '\n':
    Code:
    int ch;
    while ((ch = getchar()) != EOF && ch != '\n') {
      /* Do nothing */
    }
    Depending on your idea of flushing the buffer, you can modify that. However, keep in mind that flushing stdin alone may not be enough if other buffers outside of the program's control have unread data. Also keep in mind that flushing stdin is grossly antisocial.
    My best code is written with the delete key.

  7. #7
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    I've been looking at string.h.. the strtok function is in fact there, but vc++ says strtok undifined... never mind

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but vc++ says strtok undifined...
    The problem is probably with how you use it then.
    My best code is written with the delete key.

  9. #9
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Using VC++, I printed the values of the FILE structure field on screen with and without the buffers empty. The only diference was the cnt field that changed.
    I tried this function:
    Code:
    void flushstdin(){
    	stdin->_cnt = 0;
    }
    And it worked fine, though I don't know if this is safe to do.
    I think using the cicle with the getchar() is, sorry no offense, stupid, because when reading input from the keyboard, getchar() never return EOf, and if the buffers are empty the user HAS to press a key, plus in general there are no guarantees of finding a '\n', unless the program implicitly requires so to accept input. So there should be a better way to clear those buffers... almost like this one.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    All buffered input will contain a newline. Unless you're using non-buffered IO, they have to press enter for the information to be read from the buffer at all. Otherwise it will just sit there. Don't believe me? Run this, but don't hit enter. Post back when it reaches the end with you not hitting enter or inputting EOF.
    Code:
    #include <stdio.h>
    int main( void )
    {
        getchar( );
        printf("If you're here, you've hit enter, no two ways about it.\n");
        return 0;
    }
    So in short, you have to hit enter, and as such, it will be in the input stream. Additionally, anything you input before the newline will be buffered also. Thus, the standard example, as per the FAQ, is what you want to clear your input buffer.
    Code:
    while( (c=getchar())!='\n' && c != EOF );
    And yes, getchar can return EOF, as can be clearly demonstrated.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    stdin->_cnt = 0;
    }
    And it worked fine, though I don't know if this is safe to do.

    No it isn't safe, nor is it portable.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    sorry,
    I was lost. didn't understand the bottum line of the solution can someone please get me back on track?

    TIA,

    Ronen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  4. Template Array Class
    By hpy_gilmore8 in forum C++ Programming
    Replies: 15
    Last Post: 04-11-2004, 11:15 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21