0 Replies - 481 Views - Last Post: 28 February 2018 - 11:35 PM Rate Topic: -----

#1 XtremeNovice   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 05-February 18

2D array, genre in first column, length of song in second column? In C

Posted 28 February 2018 - 11:35 PM

I cannot figure this out. I've tried everything I can think of, I'm extremely new to this
so, ..yea.
The rows correspond with the number of songs and column 1 is genre while column 2 is the
length of song. I've tried to just declare the array int playlist[numSongs][2]; and it doesn't
work with my for loops. I forgot everything else I tried. Please help. Thanks in advance.


  6 #include <stdio.h>
  7
  8 int main (void){
  9
 10    int numSongs, gen_length=2;
 11    int i=0, j=0, k=0, genre, length;
 12
 13    fprintf(stdout, "How many songs are in your playlist?\n\n");
 14    fscanf(stdin,"%d", &numSongs);
 15
 16    //playlist array can have numerous songs but is only supposed to have
 17    //two columns. The rows correspond with the number of songs and column
 18    //one is genre while column two is the length of song
 19    int playlist[numSongs][gen_length];
 20
 21    //unfinished while loop with no update because it is not yet my main focus
 22    while(numSongs != 0){
 23       fprintf(stdout, "\ngenre:\n");
 24       fprintf(stdout,   "  1 for rock\n   2 for blues\n  3 for indie\n"
 25                         "  4 for rap\n 5 for dance/electronic\n"
 26                         "  6 for raggae\n 7 for country\n   8 for r&b\n"
 27                         "  9 for pop\n");
 28       fprintf(stdout,"--> ");
 29       fscanf(stdin,"%d", &genre);
 30       fprintf(stdout,"  Length of song: ");
 31       fscanf(stdin,"%d", &length);
 32
 33
 34       //where i am trying to add loops to put genre in first column
 35       //and length of song in the next. I've tried so many different things
 36       //also google has no clue what I'm asking it
 37       for(i=0; i < numSongs; i++){
 38          //FIXME
 39          for(j=i+1; j <= gen_length -1; j++){
 40             playlist[numSongs][gen_length]= genre;
 41          }//FIXME
 42          for(k=j+1; k <= gen_length; k++){
 43             playlist[numSongs][gen_length]= length;
 44          }
 45       }
 46       //Just printing array so i can know what its doing, not part of
 47       //code
 48       for(i=0; i < numSongs; i++){
 49          printf("\n%d", playlist[numSongs][2]);
 50       }
"playlist2.c" 54L, 1674C written                                  6,1           55%







Output ------------------------------------------------------------------------

How many songs are in your playlist?

6

genre:
1 for rock
2 for blues
3 for indie
4 for rap
5 for dance/electronic
6 for raggae
7 for country
8 for r&b
9 for pop
--> 3
Length of song: 149

3
3
3
3
3
3
genre:
1 for rock
2 for blues
3 for indie
4 for rap
5 for dance/electronic
6 for raggae
7 for country
8 for r&b
9 for pop

Is This A Good Question/Topic? 0
  • +

Page 1 of 1