Arrays Program...help Me!

This is a discussion on Arrays Program...help Me! within the C Programming forums, part of the General Programming Boards category; I have spent days and days on this program and I am getting nowhere. This is what I have: Code: ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    4

    Arrays Program...help Me!

    I have spent days and days on this program and I am getting nowhere. This is what I have:
    Code:
    #include <stdio.h>
    
    int delete_element(int a[20], int *n, int item, int loc);	/*Define function and array*/
    int d;
    int main()
    {
    	int cnt=0, a[20], *n, p, I;
    	while(*n<=20) 
    	{
    		printf("Enter some integers (maximum of 20): \n");
    	}
    	scanf("%d", &a[*n]);
    	if(cnt<=*n)
    	{
    		delete_element(a[20], n, cnt + 1, cnt);
    		printf("\n");
    		if(d>1)
    		{
    			printf("Value %d: %d copies are deleted.", *n, d);
    		}
    	}
    	else
    	{
    		printf("Value %d: %d copy is deleted.", *n, d);
    		p=p+d;
    	}
    	cnt= cnt + 1;
    
    printf("The resulting array is: \n");
    for(I=0; I <=p; I ++)
    {
    	printf("%d", a[I] );
    }
    }
    int delete_element(int a[], int *n, int item, int loc);
    {
    	int x, y, d=0, l;
    
    	for(x=loc; x<n; x=y)
    	{
    		y=x;
    		if(a[x]==item)
    		{
    			for(l=x; l<n-1; l++) a[l]=a[l+1];
    			d++; n--;
    		} 
    		else y++;
    	}
    
    return d;
    }



    ________________-

    I get an errpr with my brackets! LOL. I have gone thru it a million times and even drawn out those stupid chart/graphs that we do in class.

    Some hints would be awesome....thanks guys!

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    You've got an int main() but it doesn't return anything, try this:

    Code:
    ...printf("The resulting array is: \n");
    for(I=0; I <=p; I ++)
    {
    	printf("%d", a[I] );
    }
    
    return 0;
    } // <-- Right here your main closes, but it hasn't returned anything, so add the line above
    
     
    //Spae it out so I can see ;)
    
    int delete_element(int a[], int *n, int item, int loc);
    {
    May not be the only problem, but I hope that helps!
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    4

    Smile Thank you!

    Thank you so much for the help!!! =o) As soon as I get better at C, I can help other freshmen in C next semester on here...lol. =o)

    The only two errors I'm getting now are:

    1.) prog5.c(22) : error C2107: illegal index, indirection not allowed

    ....which I thought I had corrected by removing the '&'...those aren't supposed to be used in arrays with scanf.

    and

    2.) prog5.c(50) : error C2449: found '{' at file scope (missing function header?)

    ...i take it I have too many or too less of {}{}{} ?? lol. =o)

    Thank you guys sooo much again for your help! I didn't think this forum would be of any use to a beginner like me. =o) i wuz wrong.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    4

    oops...

    it may help if i include my code =o):
    Code:
    #include <stdio.h>
    
    int delete_element(int a[], int *n, int item, int loc);	/*Define function and array*/
    int g;
    int main()
    {
    	int cnt=0, a[20], *n, p, I;
    	while(*n<=20) 
    	{
    		for (n=0; n<20; n++) 
    		{
        		printf("Enter some integers (maximum of 20): \n");
        		scanf ( "%d", a[n] );
    		}
        }
    	scanf("%d", &a[*n]);
    	if(cnt<=*n)
    	{
    		g = delete_element(a, n, cnt+1, cnt);
    		printf("\n");
    		if(g>1)
    		{
    			printf("Value %d: %d copies are deleted.", *n, g);
    		}
    	}
    	else
    	{
    		printf("Value %d: %d copy is deleted.", *n, g);
    		p=p+g;
    	}
    	cnt= cnt + 1;
    
    printf("The resulting array is: \n");
    for(I=0; I <=p; I ++)
    {
    	printf("%d", a[I] );
    }
    return g;
    }
    int delete_element(int a[], int *n, int item, int loc);
    {
    	int x, y, g=0, l;
    
    	for(x=loc; x<n; x=y)
    	{
    		y=x;
    		if(a[x]==item)
    		{
    			for(l=x; l<n-1; l++) a[l]=a[l+1];
    			g++; n--;
    		} 
    		else y++;
    	}
    
    return g;
    }
    Code tags once agian added by Kermi3

  6. #6
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Once again:

    I don't like being mean at all, but don't just ignore someone's (my) post because it doesn't give you the answer. I am trying to help you get the answer to you r question and future ones. In case you didn't get it the first time here's the message again:


    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    4

    Unhappy Sorry!

    i'm so sorry! I forget to put my code in and totally forgot about that when i pasted it!

    i will do it from now on.

  8. #8
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    Great, thanks.
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>int *n;
    >>while(*n<=20)
    Here n is an int pointer, and you are comparing the value it points to against 20. But when did assign a value to *n ? You didn't so its uninitialised.

    >>int delete_element(int a[], int *n, int item, int loc);
    This has an extra semi-colon on it. You don't need it on the function header (but you do on the prototype)

    I fixed the pointers in this section, but I don't understand why you are doing it like this:
    Code:
    while (*n <= 20)
        {
            for (*n = 0; *n < 20; (*n)++)
            {
                printf("Enter some integers (maximum of 20): \n");
                scanf("%d", &a[*n]);
            }
        }
    Why not like this:
    Code:
        int n;
        for (n = 0; n < 20; n++)
        {
            printf("Enter some integers (maximum of 20): \n");
            scanf("%d", &a[n]);
        }
    This will will ask for 20 ints though, there's no way of stopping at say 15 without additional code. Hey, isn't this what Salem already posted?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Replies: 3
    Last Post: 01-14-2003, 10:34 PM
  3. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 06:50 AM
  4. Replies: 0
    Last Post: 10-29-2001, 11:40 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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