Beginner C question
Page 1 of 1
Monoblaine




Posts: 205

PostPosted: Sat, 19th Nov 2011 16:41    Post subject: Beginner C question
hey folks, I was hoping you could guys could help me out with this assignment. Basicly what I have to do is user has to enter numbers into 2 dimensional array and the program is supposed to print out the largest most common number and its index. I've got this far and now am kinda stuck trying to get indexes to the numbers in array, so it would be awesome if you guys could help out and explain how it is done. Here's where I am so far.
Thanks in advance Smile

Code:


#include <stdio.h>

int main(void)
{
    int N, M;
    printf("Enter desired rows\n");
    printf("Desired rows: ");
    scanf("%d", &N);
    printf("\n");
    printf("Enter desired columns (Number must be between 1 and 10)\n");
    printf("\n");
    printf("Desired columns: ");
    do{
              scanf("%d", &M);
              if(M<1 || M>10){
              printf("Entered number is incorrect, please try again.\n Number is supposed to be between 1 and 10.");
              }
              else if(M>=1 || M<=10){
              printf("You entered numbers correctly!\n");
              }
      }
    while(M<1 || M>10);
    printf("\n Enter numbers into array: ");
    int A[N][M], i, j;
    for(i=0;i<N;i++){
        for(j=0;j<M;j++){
    scanf("%d", &A[i][j]);
    }
    }
    for(i=0;i<N;i++){
        for(j=0;j<M;j++){
        printf("%d ", A[i][j]);}
        printf("\n");
        }   

getch();
return 0;
}
Back to top
Frant
King's Bounty



Posts: 24433
Location: Your Mom
PostPosted: Sat, 19th Nov 2011 22:17    Post subject:
Man, C sure looks ugly these days after getting used to managed OO-languages. Wink


Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn!

"Thank you to God for making me an Atheist" - Ricky Gervais
Back to top
Monoblaine




Posts: 205

PostPosted: Sun, 20th Nov 2011 20:39    Post subject:
Okey, so I managed to get indexes before the entered numbers but how to get the most common number in the array with its index to print on screen is beyond me, so some help with that would really be appriciated (Example as well: user enters numbers 4 6 3 7 6 4 2 6 9 so it is supposed to print that number 6 is common number and it was entered as 2nd 5th and 7th number) Here's the code so far.

Code:
#include <stdio.h>

int main(void)
{
    int N, M;
    printf("Enter desired rows\n");
    printf("Desired rows: ");
    scanf("%d", &N);
    printf("\n");
    printf("Enter desired columns (Number must be between 1 and 10)\n");
    printf("Desired columns: ");
    do{
              scanf("%d", &M);
              if(M<1 || M>10){
              printf(""Entered number is incorrect, please try again.\n Number is supposed to be between 1 and 10\n\n.");
              }
              else if(M>=1 || M<=10){
              printf("You entered numbers correctly!\n");
              }
      }
    while(M<1 || M>10);

    int A[N*M][2], i;

    for(i=0;i<N*M;i++){
        A[i][0]=i+1;
            printf("Enter numbers into array %d: ", i+1);
            scanf("%d", &A[i][1]);
            }
     for(i=0;i<N*M;i++){
      printf("%d - %d\n", A[i][0], A[i][1]);
      }


getch();
return 0;
}


thanks in forward
Back to top
WhiteBarbarian




Posts: 6003
Location: Russia
PostPosted: Mon, 21st Nov 2011 03:56    Post subject:
You would need to index that array, dictionary would do nicely. Numbers will be keys and count ala how many times number appears in array will be values.

http://stackoverflow.com/questions/4864453/associative-arrays-in-c

http://www.mailsend-online.com/blog/a-simple-associative-array-library-in-c.html


Back to top
[mrt]
[Admin] Code Monkey



Posts: 1338

PostPosted: Sat, 26th Nov 2011 01:03    Post subject:
C or C++?

C++ has a nice little STL library that houses map (an associative array).

Otherwise you have to reinvent the wheel, which is a bummer. I'm alittle late so if you still need a hand post a cry for help.


teey
Back to top
Monoblaine




Posts: 205

PostPosted: Sat, 26th Nov 2011 08:00    Post subject:
I'm good now, got it done. Thanks for your help WhiteBarbarian Smile
it was just in C. Really wasn't the best assignment
Back to top
Monoblaine




Posts: 205

PostPosted: Tue, 28th Feb 2012 10:26    Post subject:
okey bumbing up this thread a little. What i have is an input file consisting of 3 different types of elements.. input file constists of <name> <age> <salary> and i have to get <name> into 1 structure, <age> into 2nd and <salary> to third structure. I have this so far but whenever i try to print it the program just shows some random numbers and then crashes. Could use some help again Smile.

Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


struct database{
       char namei;
       int age;
       float salary;
};
struct database data[10];

void sisselugemine(struct database *andmed)
{
     FILE *pfile;
     char tahemark[51];
     pfile = fopen("input.txt", "r");
     if (pfile == NULL){
     perror ("Faili avamine ebaonnestus");
     }
     else
     {
         int i=0;
         while ( fgets (tahemark, sizeof(tahemark), pfile ) != NULL )
         {
              int k=0;
              for(int j=0;j<strlen(tahemark);j++){
                      if(tahemark[j]==" "){
                              k++;

                      }
                      switch(k){
                                case 0:
                                     andmed[i].name=tahemark[j];
                                     break;
                                case 1:
                                     andmed[i].age=tahemark[j];
                                     break;
                                case 2:
                                     andmed[i].salary=tahemark[j];
                                     break;
                      }
              }
              i++;
              printf("%s %d %f ", andmed[i].name, andmed[i].age, andmed[i].salary);
          }
      }
      fclose(pfile);
}
Back to top
Veki




Posts: 380
Location: Croatia
PostPosted: Tue, 28th Feb 2012 12:30    Post subject:
Code:

struct database{
       char namei;
       int age;
       float salary;
};


You have only 1 character for name. Don't you need a char array to store complete name?
for example char[128] - a char array for 128 characters

Code:

              for(int j=0;j<strlen(tahemark);j++){
                      if(tahemark[j]==" "){
                              k++;
                      }
                      switch(k){
                                case 0:
                                     andmed[i].name=tahemark[j];
                                     break;
                                case 1:
                                     andmed[i].age=tahemark[j];
                                     break;
                                case 2:
                                     andmed[i].salary=tahemark[j];
                                     break;
                      }
              }

This will not work.
Case 0 is constantly overwriting name variable if name is longer than 1 character.
Case 1 will write ASCII code in integer variable.
Case 2 will write ASCII code in float variable.

If you know the format your variable are in text file you should use is fscanf.
http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/


Beware of he who would deny you access to information, for in his heart he dreams himself your master.
Commisssioner Pravin Lal
"U.N. Declaration of Rights"
Back to top
Axeleration




Posts: 814

PostPosted: Thu, 1st Mar 2012 03:09    Post subject:
haha fellow estonian Smile

I thought by now eduction has moved on almost completely to OO based languages.
Back to top
Page 1 of 1 All times are GMT + 1 Hour
NFOHump.com Forum Index - Programmers Corner
Signature/Avatar nuking: none (can be changed in your profile)  


Display posts from previous:   

Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB 2.0.8 © 2001, 2002 phpBB Group