2D array variable length array?

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 651 Views - Last Post: 06 March 2018 - 07:39 AM Rate Topic: -----

#1 XtremeNovice   User is offline

  • New D.I.C Head

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

2D array variable length array?

Posted 01 March 2018 - 08:34 AM

Please Help. I have a playlist array.
In the first dimension in the array is the number of songs
in the second dimension is the genre in the first column and length of song in the second column.

I have no clue how to enter information into the second column. I think i have the first column right.

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%

 

My output looks like its printing the genre (indicated by -->) a number of 6 times which is
numSongs in the playlist[numSongs][gen_length] array. I think this is somewhat correct because column
1 is supposed to be genre, but I have no clue how to get the length of song into the second column in the array.
in my mind, the way I wrote it, it should look like this
3149
3149
3149
3149
3149
3149

this is probably still a bit incorrect but at least I will know that it is reading the input for the length of song.
my for loop to read in this information is probably incorrect.

for(i=0; i < numSongs; i++){ //this loop is for the number of rows
38 //FIXME
39 for(j=i+1; j <= gen_length -1; j++){ //this loop is to read in the genre in column 1
40 playlist[numSongs][gen_length]= genre;
41 }//FIXME
42 for(k=j+1; k <= gen_length; k++){ //this loop is supposed to read in the song length in column 2
43 playlist[numSongs][gen_length]= length;

I've played around with whether the array should be playlist[numSongs][gen_length] or
playlist[numSongs][2] and neither of them give me the results I'm looking for. Please
help. My output from the program is below.

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
  • +

Replies To: 2D array variable length array?

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7183
  • View blog
  • Posts: 14,972
  • Joined: 16-October 07

Re: 2D array variable length array?

Posted 01 March 2018 - 08:55 AM

This interesting bit of nastiness is utterly, profoundly, wrong:
printf("\n%d", playlist[numSongs][2]);



First, I'd strongly recommend a struct for this:
typedef struct {
    int genre;
    int length;
} Song;



However, if you MUST do this, I'd start with:
#define SONG_GENRE 0
#define SONG_LENGTH 1
#define SONG_COLUMNS 2

// helper function...  actually, you should have a lot more functions
int getGenreFromUser();


int main() {
    int numSongs, i;
    fprintf(stdout, "How many songs are in your playlist?\n\n");
    fscanf(stdin,"%d", &numSongs);
    int playlist[numSongs][SONG_COLUMNS];

    // wrong while(numSongs != 0){
    for(i=0; i<numSongs; i++) {
        playList[i][SONG_GENRE] = getGenreFromUser();
        fprintf(stdout,"  Length of song: ");
        fscanf(stdin,"%d", &(playList[i][SONG_LENGTH]));
    }
    // print stuff
    for(i=0; i < numSongs; i++) {
        int genre = playList[i][SONG_GENRE];
        int length = playList[i][SONG_LENGTH];
        // you take it from here



Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#3 XtremeNovice   User is offline

  • New D.I.C Head

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

Re: 2D array variable length array?

Posted 01 March 2018 - 09:10 AM

Well I don't know anything about structures. I am a student still learning. and the printf statement you're referring to was the only way I can think of printing the array so I could see what is going on. This project is broken into two parts, the first part is to get everything written into the array and the 2nd part of the project is to seperate everything into functions, which I have never done before so that will also be extremely hard.

Thank you for your reply
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7183
  • View blog
  • Posts: 14,972
  • Joined: 16-October 07

Re: 2D array variable length array?

Posted 01 March 2018 - 10:25 AM

If you are allowed to use them, look up structures on your own. https://www.tutorial..._structures.htm

To be clear, yes, printf is the way to go.

Let's try something easier. Let's assume the user has already entered some data. Can you write a print for it? Can you then move it to a function?

Start here:
#include <stdio.h>

#define SONG_GENRE 0
#define SONG_LENGTH 1
#define SONG_COLUMNS 2

int main() {
    int i;
    const int numSongs = 3;
    int playlist[numSongs][SONG_COLUMNS] = { { 1,2 },{ 3,4 },{ 5,6 } };

    // print stuff
    for (i = 0; i < numSongs; i++) {
        printf("%d %d\n", playList[i][SONG_GENRE], // you do the rest
    }
    return 0;
}



Once you have that working, then:
#include <stdio.h>

#define SONG_GENRE 0
#define SONG_LENGTH 1
#define SONG_COLUMNS 2

void showSongs(int playlist[][SONG_COLUMNS], int);

int main() {
    const int numSongs = 3;
    int playlist[numSongs][SONG_COLUMNS] = { { 1,2 },{ 3,4 },{ 5,6 } };
    showSongs(playlist, NUM_SONGS);
    return 0;
}

void showSongs(int xs[][SONG_COLUMNS], int size) {
    int i;
    for (i = 0; i < size; i++) {
        // ...



This post has been edited by baavgai: 01 March 2018 - 12:59 PM
Reason for edit:: Just noted bad tagging

Was This Post Helpful? 0
  • +
  • -

#5 XtremeNovice   User is offline

  • New D.I.C Head

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

Re: 2D array variable length array?

Posted 01 March 2018 - 12:37 PM

I'm still learning the basics of functions as we speak, starting to get a better idea of how to code it. This is helping alot. Really appreciate it.
Was This Post Helpful? 0
  • +
  • -

#6 XtremeNovice   User is offline

  • New D.I.C Head

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

Re: 2D array variable length array?

Posted 01 March 2018 - 12:52 PM

But no, I don't know how to do any of that but its part of the project so eventually I'll have to figure it out.
Was This Post Helpful? 0
  • +
  • -

#7 XtremeNovice   User is offline

  • New D.I.C Head

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

Re: 2D array variable length array?

Posted 04 March 2018 - 10:18 AM

THIS IS THE SAME PROJECT... THESE ARE THE INSTRUCTIONS BELOW.... I KNOW ALL OF THE FIXME'S ARE WRONG

main() consists of 5 lines. the following functions are mandatory

The fillArrays() function will be called from within the main() function. The first argument is the number of songs entered by the user and will be used as a sentinel value for the loop inside this function which will get the genre and the length of song values, putting them into the array, which is the second argument. I have two local variables declared inside this function.

The getInfo() function will be called from within the main() function. The first argument, again, is the number of
songs entered by the user, and the array is the second argument. Inside this function, you will traverse the array, once for each genre, each time getting the information needed from the array to calculate and display the summary information. The print statements that show the summary information will be printed from within this function, after the information for each genre is determined. I (the instructor) have a total of 13 variables in this function.

The getSongType() function will be called from within the getInfo() function (to reduce the number of lines in the
getInfo() function). The first argument is the genre number, and the second argument is a character array, or string, to hold the string for the name of the genre. So, if the genre number is 1, then the array will hold the string “rock”. You have the choice of using a switch statement or an if-else-if statement in this function. The purpose of this function is to give you practice with calling a function from within another function other than the main() function. No other local variables are needed in this function.

/*ADDDDDDDDDDDDD HEADER PLEASE AND THANK YOU*/

#include <stdio.h>
#include <string.h>
#define SONG_GENRE 0
#define SONG_LENGTH 1
#define SONG_COLUMNS 2


void fillArrays(int howMany, int songs[][2]);
void getInfo(int howMany, int songs[][2]);
void getSongType(int genreNum, char songType[45]);


int main() {
	int numSongs;//correlates to howMany
	
	fprintf(stdout, "How many songs are in your playlist?\n\n");
	fscanf(stdin,"%d", &numSongs);
 	
	int playlist[numSongs][2];

	fillArrays(numSongs, playlist);//function call: numSongs is howMany
	getInfo(numSongs, playlist);//function call: playlist is songs array
		
	return 0;
}


void fillArrays(int howMany, int songs[][2]){//for loop and printf
	int i=0; 
	for(i=0; i < howMany; i++){
		fprintf(stdout, "\ngenre:\n");
		fprintf(stdout,"	1 for rock\n	2 for blues\n	3 for indie\n"
							"	4 for rap\n	5 for dance/electronic\n	6 for raggae\n"
							"	7 for country\n	8 for r&b\n	9 for pop\n");
		fprintf(stdout,"--> ");
		fscanf(stdin,"%d", &(songs[i][SONG_GENRE]));
	
		fprintf(stdout,"  Length of song: ");
		fscanf(stdin,"%d", &(songs[i][SONG_LENGTH]));
	}
}


void getInfo(int howMany, int songs[][2]){
	int genNumber=0, i=0, j=0, lengthSum=0, avgValue=0;// minLength=0,
	//maxLength=0; genre=0, length=0;//genNumber=genreNum
	int minValue = 999, maxValue = 0, totalSongs=0;
	//const int STR_SIZE = 45;
	char songName[45]; //SUPPOSED to correlate to songType
	int songSum =0; //SUPPOSED to total individual genre like a counter
 
	
	getSongType(genNumber, songName);//supposed 2 traverse array, dk how to test
	
	for(i=0; i < howMany -1; i++) {//FIXME DK if this is working 
		//songs[i][SONG_GENRE] = getSongType();//DK what this is
		//songs[i][SONG_LENGTH] = getSongType();
		for(j=1; j < howMany; j++){//FIXME not working for min or max
			if(SONG_LENGTH < minValue){
				minValue = SONG_LENGTH;
			}
			if(SONG_LENGTH > maxValue){
				maxValue = SONG_LENGTH;
			}	
			
		}	
		for(i=0; i < howMany; i++) {
			lengthSum += SONG_LENGTH;
			avgValue = (lengthSum / howMany);
			totalSongs += 1;
		} 
		/*FIXME songSum not working and this summary prints regardless of which
		genre its supposed to print a summary for each genre but idk how to make
		the if statements in getSongType to work for this thats why its
		incomplete*/
		fprintf(stdout, "Your playlist of %d songs has:\n", totalSongs);
		fprintf(stdout, "%3.d %s songs\n	shortest was %d:%.2d\n"
			"	longest was %d:%.2d\n	average length %d:%.2d\n\n",
			/*FIXME i know this shouldnt be SONG_LENGTH but idk what it should be
			none of these arguments work except avgValue.*/
			songSum, songName, SONG_LENGTH/60, SONG_LENGTH%60, SONG_LENGTH/60, 
			SONG_LENGTH%60, avgValue/60, avgValue%60);
	}
}


void getSongType(int genreNum, char songType[45]){
	/*FIXME of course char isnt passing thru don't know how to make array assign
	to songName in other function or how to make if else if/switch work the
	whole function is incomplete. genreNum is supposed to somehow relate to
	SONG_GENRE so that depending on what number was entered, that genre will
	print the summary*/
	
	/*if (genreNum > 0 && genreNum == 1){//orginally genreNum or SONG_GENRE
		//char songType [45] = "Rock";//commented out just so i have no warnings
	}
	if (SONG_GENRE > 0 && SONG_GENRE == 2){
		//char songType [] = {"blues"};
		printf("#2");
	}*/
}



The output is below... after it says "Your playlist of 3 songs has:" its supposed to say the 3 rock songs. but i cant get the string to pass thru the other functions and when I try to initialize it I get a multitude of different errors. the avgValue was calculating correctly but its not anymore. the min and max never worked. and if I chose a different genre the same summary would print but its supposed to print a different summary for each genre. this project does not cover structures, I am only supposed to use these functions, I can add more but how can I do that if I cant get the ones I'm supposed to use to work first.

OUTPUT-----------------------------------------------------

How many songs are in your playlist?

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
--> 1
Length of song: 222

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
--> 1
Length of song: 444

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
--> 1
Length of song: 333
Your playlist of 3 songs has:
songs
shortest was 0:01
longest was 0:01
average length 0:01
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14100
  • View blog
  • Posts: 56,520
  • Joined: 12-June 08

Re: 2D array variable length array?

Posted 04 March 2018 - 11:16 AM

Ok - what have you taken to get those 'fixmes' from your teacher, righted?
Was This Post Helpful? 0
  • +
  • -

#9 XtremeNovice   User is offline

  • New D.I.C Head

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

Re: 2D array variable length array?

Posted 04 March 2018 - 11:51 AM

I don't understand your question. All my teacher is going to say is "I think you should start smaller and work from there".
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14100
  • View blog
  • Posts: 56,520
  • Joined: 12-June 08

Re: 2D array variable length array?

Posted 04 March 2018 - 11:52 AM

Never mind, I misunderstood that last post.
Was This Post Helpful? 0
  • +
  • -

#11 XtremeNovice   User is offline

  • New D.I.C Head

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

Re: 2D array variable length array?

Posted 04 March 2018 - 11:58 AM

Im completely stuck on this project and could use help. i have 4 main issues. the first issue is I'm trying to get the getInfo function to take in this array of strings from the getSongType function. if the user types a 1 then rock should be printed. it has to be called from getInfo...
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14100
  • View blog
  • Posts: 56,520
  • Joined: 12-June 08

Re: 2D array variable length array?

Posted 04 March 2018 - 12:51 PM

The lines are pointed out.. I would start there.

Example:
057	    for(i=0; i < howMany -1; i++) {//FIXME DK if this is working 


Think about it. Your condition asks to start at 0 and repeat if the loop control variable is less than 'homany-1'.. If 'howmany' is 1 then 1 - 1 is 0.. which means your loop doesn't happen because 0 is never less than 0.

tldr; your loop doesn't work at the minimal value and larger values you miss the entire last iteration.

Example:
060	        for(j=1; j < howMany; j++){//FIXME not working for min or max


Again, think if your 'howmany' was 1. If j starts at 1 then j < howmany - or 1 < 1 - would never be true and the loop would never work.
Was This Post Helpful? 0
  • +
  • -

#13 XtremeNovice   User is offline

  • New D.I.C Head

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

Re: 2D array variable length array?

Posted 04 March 2018 - 01:03 PM

for(i=0; i < howMany -1; i++)
I originally had i < howMany and that didn't work either
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14100
  • View blog
  • Posts: 56,520
  • Joined: 12-June 08

Re: 2D array variable length array?

Posted 04 March 2018 - 01:04 PM

Pretty sure it would work better.
Was This Post Helpful? 0
  • +
  • -

#15 XtremeNovice   User is offline

  • New D.I.C Head

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

Re: 2D array variable length array?

Posted 04 March 2018 - 01:07 PM

okay... so I changed it back and its still not doing the min and max.....
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2