13 Replies - 1418 Views - Last Post: 12 March 2011 - 03:01 AM Rate Topic: -----

#1 Top_Noob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-February 11

Array problem in C programming

Posted 11 March 2011 - 06:43 PM

Okay, I'm new here and I'm try to make a program (obviously) but it's been giving me a few problems. One problem I'm having is trying to get the program to print all the elements in a user populated array, this isn't the finished code but the counter in the code is for the bounded iteration/for loop I'm gonna use. I need to know what I'm doing wrong, when I try to run the code and I get to the part where the program is supposed to print the elements inside of the array, it prints some random number. What did I do wrong? Also if there's any thing in my code that you think can be written differently feel free to give your input.

#include <stdio.h>
#include <conio.h>

char loanlevel(int);
int level1check(float);
int level2check(float);
int level3check(float);
int main()
{
    int num1;
    int denied;
    char fn[10];
    char ln[10];
    int trn;
    int ll;
    float mi;
    char r;
    int lev;
    int counter =1;
    int accepted[10];
    

    printf("***********************************************************************\n");
    printf("*                      Welcome to CORE Corporation                    *");
    printf("\n***********************************************************************\n"); 
    printf("\n\n*              This program will carry you through a step             *\n*              by step process to help you fill out you               *");
    printf("\n*              mortgage application. Please enter your                *\n*              information as accurately as possible.                 *\n");
    printf("\n\n*                    Please enter your full name.                     *\n");
    scanf("%s%s", &fn, &ln);
    printf("\n*                    Please enter your TRN number.                    *\n");
    scanf("%d", &trn);
    printf("\n*   Please enter the number corresponding to your desired loan level  *");
    printf("\n ----------------------------------------------------------------------");
    printf("\n|                NUMBERS FOR CORRESPONDING LOAN LEVELS                |");
    printf("\n|                       Level 1      :       1                        |"); 
    printf("\n|                       Level 2      :       2                        |"); 
    printf("\n|                       Level 3      :       3                        |"); 
    printf("\n ----------------------------------------------------------------------\n");
    scanf("%d", &ll);
    printf("\n*                    Please enter your monthly income.                *\n");
    scanf("%f", &mi); 
    r = loanlevel(ll);
    switch(r)
    {
    case 'A':
       lev = level1check(mi);
       if (lev == 123)
       {  
         printf("You've been accepted!");
         accepted[counter]=trn;
       }
       else
       {
           printf("You've been denied.");
       }   
       break;
    case 'B':
       lev = level1check(mi);
       if (lev == 123)
       {
           printf("You've been denied.");
       }  
       else
       {
         lev = level2check(mi);
         if (lev == 456)
           {
             printf("You've been accepted!");
             accepted[counter]=trn;
           }
         else
           {
             printf("You've been denied");
           }
        }
        break;            
    case 'C':
       lev = level1check(mi);
       if (lev == 123)
       {
           printf("You've been denied.");
       }   
       else
         {
           lev = level2check(mi);
           if (lev == 456)    
           {     
             printf("You've been denied");
           }            
           else
             {    
                lev = level3check(mi);
                if (lev == 789)
                  {
                    printf("You've been accepted!");
                    accepted[counter]= trn;
                  }
                else
                  {
                    printf("You've been denied");
                  }
             } 
         }   
       break; 
    default: 
       printf("You've been denied.");
       break;
    }
    fflush(stdin);
    printf("\n\nThe TRN of the people who loans where granted to are:\n%d", accepted);
    getchar();
    getchar(); 
}




Is This A Good Question/Topic? 0
  • +

Replies To: Array problem in C programming

#2 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Array problem in C programming

Posted 11 March 2011 - 07:59 PM

I guess I'll never know why people think they should use
fflush(stdin}
or where they even get the idea to do that.

Although it does compile, the behavior of fflush(stdin) when it runs is undefined, i.e. any kind of bad thing can happen. So don't do it.

As to your array
- array indexing starts at 0, not 1, so the initial value of your counter should be 0.
- the name of the array (accepted in your case) acts as the memory address of the first element of the array, but that's not the same as the value of the element. printf needs an actual value as its argument, so to print the element indexed by your counter you should write
printf("\n\nThe TRN of the people who loans where granted to are:\n%d", accepted[counter]);
That will work correctly as long as counter is the index of an array element where you have actually stored something (because if you haven't actually stored something there, it will contain some "random garbage" left over from some previous use of that memory address.

PS the "random number" that you were getting was the memory address of the beginning of the array.
Was This Post Helpful? 1
  • +
  • -

#3 Top_Noob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-February 11

Re: Array problem in C programming

Posted 11 March 2011 - 08:22 PM

View Postr.stiltskin, on 11 March 2011 - 07:59 PM, said:

I guess I'll never know why people think they should use
fflush(stdin}
or where they even get the idea to do that.

Although it does compile, the behavior of fflush(stdin) when it runs is undefined, i.e. any kind of bad thing can happen. So don't do it.

As to your array
- array indexing starts at 0, not 1, so the initial value of your counter should be 0.
- the name of the array (accepted in your case) acts as the memory address of the first element of the array, but that's not the same as the value of the element. printf needs an actual value as its argument, so to print the element indexed by your counter you should write
printf("\n\nThe TRN of the people who loans where granted to are:\n%d", accepted[counter]);
That will work correctly as long as counter is the index of an array element where you have actually stored something (because if you haven't actually stored something there, it will contain some "random garbage" left over from some previous use of that memory address.

PS the "random number" that you were getting was the memory address of the beginning of the array.



Thanks but Its just prints 0 now.
printf("\n\nThe TRN of the people who loans where granted to are:\n%d", accepted[counter]);



 int counter =0;
    int accepted[10];
    
    for (counter = 0; counter <=1; counter++)

Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Array problem in C programming

Posted 11 March 2011 - 08:26 PM

If it prints 0, 0 is the value you have stored there. It's hard to comment on why, without knowing the exact code you're running now, and the exact input that you are giving it.
Was This Post Helpful? 0
  • +
  • -

#5 Top_Noob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-February 11

Re: Array problem in C programming

Posted 11 March 2011 - 08:28 PM

Alright, the only changes I made were the ones in the reply I posted, and the inputs were just two random numbers other than 0. Should the program print both numbers if I printed the entire array?
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Array problem in C programming

Posted 11 March 2011 - 08:37 PM

View PostTop_Noob, on 11 March 2011 - 10:28 PM, said:

... and the inputs were just two random numbers other than 0. Should the program print both numbers if I printed the entire array?

I don't know what that means. Print what numbers? How can you just input two random numbers. You have prompts for at least 5 inputs that I can see. And I have no idea what's going on in those four other functions that you haven't posted, so I can't tell whether the applicant will be accepted or not.

You only process 1 applicant, so there will be data only in the first element of the array, and only if the applicant is accepted.
Was This Post Helpful? 0
  • +
  • -

#7 Top_Noob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-February 11

Re: Array problem in C programming

Posted 11 March 2011 - 08:46 PM

Ok, the program accepts four variables at once then runs a check, if two conditions are satisfied then one of the four variables (other than the two used to test the condition) is stored in an array. The variable stored in the array is the variable "trn" this consist of a random arrangement of integers, a random number. Because I had set the program to loop twice this is done twice, and as a result values are stored in accepted[1] and accepted [2], right? Now at the end I want it to print both these numbers. Following what you told me the program went from printing some random number to printing 0, all I want is for the program is to print the two values it stored in the array.
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2034
  • View blog
  • Posts: 5,436
  • Joined: 27-December 05

Re: Array problem in C programming

Posted 11 March 2011 - 09:05 PM

In the code you posted, counter is initialized as 1. But the first element of any array is array[0], so I said to initialize counter as 0. That would mean that (at least) the first applicant's trn will be stored in accepted[0]. I don't see anything in the code that you posted that ever changes the value of counter, so as far as I can tell the second applicant's trn simply overwrites the first one, and ends up in accepted[0]. So printing accepted[0] should produce something -- but only if (a)one of the applicants was accepted and (b)trn at some point contains some valid value and ©there isn't anything wrong in the other functions that's trashing your array.

So there are various things you can check. For example, you might try printing the value of trn just for debugging purposes immediately before the lines where it is assigned to the array.
Was This Post Helpful? 0
  • +
  • -

#9 Top_Noob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-February 11

Re: Array problem in C programming

Posted 11 March 2011 - 09:32 PM

yeah, I did change it, posted it in the reply to your first reply and as a result of changing it the program starting printing 0 where it was supposed to print the values inside the array.

heres a copy of the part of the code where I changed the value in counter:
    int counter =0;
    int accepted[10];
    
    for (counter = 0; counter <=1; counter++)


and the part of the code where I tried to print all the values in the array:
 printf("\n\nThe TRN of the people who loans where granted to are;\n%d", accepted[counter]);



I think you're misunderstanding something, I dnt want to print the values of the array in the loop I want to print it out off the loop, meaning if I stored ten values in the array I want to print them all at once. Could have been done in Pascal now how do you do it in C?
Was This Post Helpful? 0
  • +
  • -

#10 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Array problem in C programming

Posted 11 March 2011 - 09:41 PM

Please post your complete code again, as it is now.
With a whole program we can help you much more effectively than hoping we glue your snippets together in the right way.
Was This Post Helpful? 0
  • +
  • -

#11 Top_Noob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-February 11

Re: Array problem in C programming

Posted 11 March 2011 - 09:49 PM

#include <stdio.h>
#include <conio.h>

char loanlevel(int);
int level1check(float);
int level2check(float);
int level3check(float);
int main()
{
    int num1;
    int denied;
    char fn[10];
    char ln[10];
    int trn;
    int ll;
    float mi;
    char r;
    int lev;
    int counter =0;
    int accepted[10];
    int i;
    
    for (counter = 0; counter <=1; counter++)
    {
    printf("***********************************************************************\n");
    printf("*                      Welcome to CORE Corporation                    *");
    printf("\n***********************************************************************\n"); 
    printf("\n\n*              This program will carry you through a step             *\n*              by step process to help you fill out you               *");
    printf("\n*              mortgage application. Please enter your                *\n*              information as accurately as possible.                 *\n");
    printf("\n\n*                    Please enter your full name.                     *\n");
    scanf("%s%s", &fn, &ln);
    printf("\n*                    Please enter your TRN number.                    *\n");
    scanf("%d", &trn);
    printf("\n*   Please enter the number corresponding to your desired loan level  *");
    printf("\n ----------------------------------------------------------------------");
    printf("\n|                NUMBERS FOR CORRESPONDING LOAN LEVELS                |");
    printf("\n|                       Level 1      :       1                        |"); 
    printf("\n|                       Level 2      :       2                        |"); 
    printf("\n|                       Level 3      :       3                        |"); 
    printf("\n ----------------------------------------------------------------------\n");
    scanf("%d", &ll);
    printf("\n*                    Please enter your monthly income.                *\n");
    scanf("%f", &mi); 
    r = loanlevel(ll);
    switch(r)
    {
    case 'A':
       lev = level1check(mi);
       if (lev == 123)
       {  
         printf("You've been accepted!\n");
         accepted[1]=trn;
       }
       else
       {
         lev = level2check(mi);
           if (lev == 456)
             {
               printf("You've been accepted!\n");
               accepted[1]=trn;
             }
           else
             {    
                lev = level3check(mi);
                  if (lev == 789)
                    {
                      printf("You've been accepted!\n");
                      accepted[1]= trn;
                    }
                  else
                    {
                      printf("You've been denied.\n");
                    }
             }      
         }
       break;
    case 'B':
       lev = level1check(mi);
       if (lev == 123)
       {
           printf("You've been denied.\n");
       }  
       else
       {
         lev = level2check(mi);
         if (lev == 456)
           {
             printf("You've been accepted!\n");
             accepted[1]=trn;
           }
         else
           {    
              lev = level3check(mi);
                if (lev == 789)
                  {
                    printf("You've been accepted!\n");
                    accepted[1]= trn;
                  }
                else
                  {
                    printf("You've been denied.\n");
                  }
            }      
         }
        break;            
    case 'C':
       lev = level1check(mi);
       if (lev == 123)
       {
           printf("You've been denied.\n");
       }   
       else
         {
           lev = level2check(mi);
           if (lev == 456)    
           {     
             printf("You've been denied\n");
           }            
           else
             {    
                lev = level3check(mi);
                if (lev == 789)
                  {
                    printf("You've been accepted!\n");
                    accepted[1]= trn;
                  }
                else
                  {
                    printf("You've been denied\n");
                  }
             } 
         }   
       break; 
    default: 
       printf("You've been denied.\n");
       break;
    }
   }
    
    for(i=0; i<10; i++){
     printf("%d\n",accepted[i]);
     }
    
    printf("\n\nThe TRN of the people who loans where granted to are;\n%d", accepted);
    
    getchar();
    getchar(); 
}





char loanlevel(int q)
{
    char e;
    if (q == 1) 
    {
      e = 'A';
    }
       else
       {
         if (q == 2) 
         {
           e = 'B';
         }
         else
         {
           if (q == 3) 
           {
             e = 'C';
           }
         }
       }
    return(e);
}  

int level1check(float r)
{ 
    int q;
    if (r >= 60000)
      {
          if (r <= 100000)
            {
              q = 123;
            } 
      }
      return(q);
}     

int level2check(float s)
{ 
    int t;
    if (s >= 100000)
      {
          if (s <= 250000)
            {
              t = 456;
            } 
      }
      return(t);
}     

int level3check(float u)
{ 
    int v;
    if (u >= 250000)
      {       
              v = 789;           
      }
      return(v);
}     


Was This Post Helpful? 0
  • +
  • -

#12 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Array problem in C programming

Posted 11 March 2011 - 09:53 PM

I see the nasty nasty 'conio.h' include.
It is adding nothing to your program except making it non-standard. Get rid of it.

Once I remove that this is what the compiler tells me about your program.
dic.c: In function ‘main’:
dic.c:31: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[10]’
dic.c:31: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char (*)[10]’
dic.c:144: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
dic.c:11: warning: unused variable ‘denied’
dic.c:10: warning: unused variable ‘num1’
dic.c:148: warning: control reaches end of non-void function



Always enable the display of warnings in your compiler.
Error reporting is not enough to write good code.
If you don't know how to enable warnings in your tool then search the help files or the Google.
You need to learn how to display warnings as a matter of urgency. Don't write another line of code until you have added this vital skill to your toolbox.
Was This Post Helpful? 0
  • +
  • -

#13 Top_Noob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-February 11

Re: Array problem in C programming

Posted 11 March 2011 - 10:11 PM

View Postjanotte, on 11 March 2011 - 09:53 PM, said:

I see the nasty nasty 'conio.h' include.
It is adding nothing to your program except making it non-standard. Get rid of it.

Once I remove that this is what the compiler tells me about your program.
dic.c: In function ‘main’:
dic.c:31: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[10]’
dic.c:31: warning: format ‘%s’ expects type ‘char *’, but argument 3 has type ‘char (*)[10]’
dic.c:144: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
dic.c:11: warning: unused variable ‘denied’
dic.c:10: warning: unused variable ‘num1’
dic.c:148: warning: control reaches end of non-void function



Always enable the display of warnings in your compiler.
Error reporting is not enough to write good code.
If you don't know how to enable warnings in your tool then search the help files or the Google.
You need to learn how to display warnings as a matter of urgency. Don't write another line of code until you have added this vital skill to your toolbox.


Okay sure, but do you have an email address so I can contact you if i need more help?

This post has been edited by Top_Noob: 11 March 2011 - 10:13 PM

Was This Post Helpful? 0
  • +
  • -

#14 janotte   User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Array problem in C programming

Posted 12 March 2011 - 03:01 AM

View PostTop_Noob, on 12 March 2011 - 03:11 PM, said:

Okay sure, but do you have an email address so I can contact you if i need more help?


You just post here and get a whole group of people to help you.
Far better to have multiple people helping you than just one.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1