1 Replies - 212 Views - Last Post: 04 March 2018 - 12:58 PM Rate Topic: -----

#1 XtremeNovice   User is offline

  • New D.I.C Head

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

passing string to user defined function PLEASE HELP!

Posted 04 March 2018 - 12:57 PM

THIS IS THE SAME PROJECT... THESE ARE THE INSTRUCTIONS BELOW.... I KNOW ALL OF THE FIXME'S ARE WRONG... PLEASE HELP ME TO FIX

PROJECT INSTRUCTIONS: (This is building off a previous project of playlist)
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

Is This A Good Question/Topic? 0
  • +

Replies To: passing string to user defined function PLEASE HELP!

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: passing string to user defined function PLEASE HELP!

Posted 04 March 2018 - 12:58 PM

Please do not create duplicate topics.

Closing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1