array of series

This is a discussion on array of series within the C Programming forums, part of the General Programming Boards category; I need to take as input positive integers 1-100. Each of those numbers will be the first element in series. ...

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

    array of series

    I need to take as input positive integers 1-100. Each of those numbers will be the first element in series. The series will be formed by a specified formula, in case of pair integer- set 1 & case of uneven integer- set 2. Then of course print it.

    I'm not quit sure about the syntax of this code & also compiler through me with many errors & warnings. Cannot seem to find why!

    Please review it and have your notes.

    Code:
    :
    
    
    
    #include<stdio.h>
    #define N 5
    #define M 5
       void seri(int A[][M],int *,int *);
      int main ( void )
    {    
                        int A[N][M],*p,*n=0;
                        p=A;   
          printf ("\nenter numbers from 1 to 100\n");
                scanf ("%d", p);
             if(*p<1||*p>100)
              {
               puts(" Number is too big/ too small. Try again!");
              }
            else
              {  
                                  while (*p!=-1)      
                                            {
                                            for (p=A;p-A<M;p++)
                                                      {
                                            puts ("next number:");
                    scanf ("%d", p++);
                    seri(A,n,p);
                    }
    }
    
    
    
              void seri(int A[N][M],int *n,int *p)
               {                                     
                int t;
                         for (p=A;p-A>=1;p++)
                       { 
                          if (*p%2==0)
                           *p= *p/2;
                         else  
                           {
                          *p=3*(*p)+1;
                            }
                   n=p-A;       
                        }                                     
    
                  printf("n= %d. seri is :", n);
    
                    for (t=0;t<=n;t++)
    
                            printf("  %d\n", *p);
    
                                   }            
    
                }
    TIA


    Ronen

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why is it that whenever anyone says they get compiler errors and warnings, they never think to post them?

    Well for starters, use [code] [/code] tags around any posted code. Had you actually taken the time to read the Announcements, you'd have known this already.

    Next off, you have an unmatched number of { } pairs. Fix your indenting, and you'd have seen that.

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

  3. #3
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Geee!!! Do you know what a pointer is?!?! Or an array?
    First error:
    Code:
    int A[N][M],*p,*n=0;
    p=A;
    The problem is you're assigning a double pointer (the two-dimension array A) to a single pointer (the var *p)
    Do like this
    Code:
    int A[N][M],*p,*n=0;
    int x=0, y=0;
    p=&(A[x][y]);
    //where x and y are the indexes of the number you want to edit
    This goes on for the entire source.

    Plus.. this is awfull: for (p=A;p-A<M;p++)
    More, you don't use *n for nothing... But I'll keep it
    In this assignment int *n=0, you saying that you have a pointer that point to memory adress 0, or in other word NULL. If you want to point something you must first allocate some memory to that pointer:
    Code:
    n=malloc(sizeof(int))
    A detail: if you write int *n=0, your defining the variable value.
    If you then write *n=0, after n being defined, then you're altering the pointed int.

    Specify better want you want to do..

  4. #4
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    Where's another one....
    Code:
    #include<stdio.h>
    #define N 5
    #define M 5
    
    void seri(int A[N][M],int x);
    
    int main(){    
    	int A[N][M],*p, n=0;
    	int x = 0, y = 0;
    	p=&(A[x][y]);
    	
    	printf ("\nenter numbers from 1 to 100\n");
    
    	while(x<N){
    		scanf("%d", &(A[x][0]));
    		if(A[x][0]<1||A[x][0]>100)
    			puts(" Number is too big/ too small. Try again!");
    		else{
    			seri(A,x);
    			x++;
    			if(x<N)	puts ("next number:");
    		}
    	}
    }
    
    void seri(int A[N][M],int x){                                     
    	int i, n;
    	
    	for(i=0;i<M-1;i++){
    		if (A[x][i]%2==0)
    			A[x][i+1]= A[x][i]/2;
    		else
    			A[x][i+1]=3*A[x][i]+1;
    		n=A[x][i+1]-A[x][0];
    	}                                     
    
    	printf("n = %d. seri is :\n", n);
    	for(i=0;i<M;i++)
    		printf("\t%d\n", A[x][i]);
    }
    Is this the seri function you wanted??
    Last edited by xErath; 06-21-2004 at 11:41 PM.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    Appologize for the uncomplete info regarding the drill:
    the series needs to begin in the entered integer at the beginnig & end with 1. Delimeter for the end of input is -1.
    like:
    input - 10
    output
    10 5 16 8 4 2 1.

    all input no. has to be entered together & all output data has to come out at the end as a bulk.

    like:

    10 5 16 8 4 2 1
    11 34 17 51 26 13 40 20 10 5 16 8 4 2 1
    .
    .
    .
    & finaly n reperents the index of elements in each line. In other words: how many numbers in line.

    hope I have cleared everything now...

    TIA.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    what do you think about this one?

    Code:
    :
    
    #include<stdio.h>
    #define N 5
    #define M 900
    
    void seri(int A[N][M],int x,int y,int n);
    
    int main()
    {    
    	int A[N][M],*p, n=0;
    	int x = 0, y = 0;
    	p=&(A[x][y]);
    	
    	printf ("\nenter numbers from 1 to 100\n");
    
    	while(x<N)
    	{
    		scanf("%d", &(A[x][0]));
    		if(A[x][0]<1||A[x][0]>100)
    			puts(" Number is too big/ too small. Try again!");
    		else
    		{
    			seri(A,x,y,n);
    			x++;
    			if(x<N)	puts ("next number:");
    		}
    	}
    }
    
    void seri(int A[N][M],int x,int y,int n)
    {                                    
    	
    	for(A[x][0];A[x][y]>1;y++)
    	{
    		if (A[x][y]%2==0)
    			A[x][y+1]= A[x][y]/2;
    		else
    			A[x][y+1]=3*A[x][y]+1;
    		n=y;
    		printf("\t%d\n", A[x][y]);
    	}                                     
    
    	printf("n = %d. seri is :\n", n);
    	
    }
    Now I miss
    1. the 1 at the end. If I write:

    Code:
    for(A[x][0];A[x][y]=>1;y++)
    I get compiler error:
    error C2059: syntax error : '>'
    error C2143: syntax error : missing ';' before ')'
    error C2181: illegal else without matching if
    error C2143: syntax error : missing ')' before 'string'
    error C2143: syntax error : missing '{' before 'string'
    error C2059: syntax error : '<Unknown>'
    error C2059: syntax error : ')'
    error C2059: syntax error : '}'


    2. the bulk printing at output mentioned above.
    Last edited by ronenk; 06-22-2004 at 04:13 AM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Switch them around. You don't put the equal sign first on the compound operators like that:
    Code:
    *=
    /=
    +=
    -=
    %=
    |=
    &=
    ~=
    <<=
    >>=
    ^=
    !=
    <=
    >=
    Quzah.
    *Yeah, I know it's not there, but read the first sentence to figure out why I didn't list it.
    **If you don't know what I'm talking about, ignore these.
    ***If you're really confused now close your browser.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    tried this too before"

    Code:
    for(A[x][0];A[x][y]>=1;y++)

    Then there is some kind of bug, or I dont know what...
    I get very long list of:

    enter numbers from 1 to 100
    6


    6
    3
    10
    5
    16
    8
    4
    2
    1
    4
    2
    1
    4
    2
    1
    .
    .

    it is longer the array size, but with respect to it. For instance on a 2*3 array, I got ~44 lines. On larger arrays- more lines etc...

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >for(A[x][0];A[x][y]>=1;y++)
    This will loop until A[x][y] is 0. Your series isn't very good at getting that particular result. Just use > and print the last number outside of the loop:
    Code:
    void seri(int A[N][M],int x,int y,int n)
    {
      for(;A[x][y]>1;y++)
      {
        if (A[x][y]%2==0)
          A[x][y+1]= A[x][y]/2;
        else
          A[x][y+1]=3*A[x][y]+1;
        n=y;
        printf("\t%d\n", A[x][y]);
      }
      printf("\t%d\n", A[x][y]);
      printf("n = %d. seri is :\n", n);
    }
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    How about the "bulk" printing in output mentioned above?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >How about the "bulk" printing in output mentioned above?
    Here's that feature along with a bit of cleaning up I added for fun:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define NSERIES    5
    #define SERIES_LEN 900
    
    int  in_range(int value, int low, int high);
    void seri(int series[SERIES_LEN]);
    
    int main(void)
    {    
      int series_list[NSERIES][SERIES_LEN];
      int x = 0;
      int y;
    
      while (x < NSERIES)
      {
        printf("Enter a number from 1 to 100: ");
        fflush(stdout);
        /* Always error check scanf */
        if (scanf("%d", &series_list[x][0]) != 1)
        {
          fprintf(stderr, "Invalid number\n");
          return EXIT_FAILURE;
        }
        if (!in_range(series_list[x][0], 1, 100))
          fprintf(stderr, "Number is too big/small. Try again!\n");
        else
          seri(series_list[x++]);
      }
      /* Bulk print */
      for (x = 0; x < NSERIES; x++)
      {
        for (y = 0; y < SERIES_LEN - 1 && series_list[x][y] > 1; y++)
          printf("%-3d ", series_list[x][y]);
        printf("%-3d ", series_list[x][y++]);
        printf("\nn = %d\n", series_list[x][y]);
      }
    
      return EXIT_SUCCESS;
    }
    
    int in_range(int value, int low, int high)
    {
      return value >= low && value <= high;
    }
    
    void seri(int series[SERIES_LEN])
    {
      int y;
      int n = 0;
    
      /* Place n in the last spot */
      for (y = 0; y < SERIES_LEN - 1 && series[y] > 1; y++)
      {
        if (series[y] % 2 == 0)
          series[y + 1] = series[y] / 2;
        else
          series[y + 1] = 3 * series[y] + 1;
        n = y;
      }
      series[++y] = n;
    }
    My best code is written with the delete key.

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    Wow, cool!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM

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