This is what I have so far. Something about this code is not correct. I think im
ID: 3813046 • Letter: T
Question
This is what I have so far. Something about this code is not correct. I think im very close to solving it. Below is my code, and attached are two screenshots, what I get and what I SHOULD get.
Thank you very much for your help! I will make sure to thumbs up the answer! Thank you!!!
#include
#include
#include
#include
#define NAME_LEN 30
struct dog{
int number;
char dog_name[NAME_LEN+1];
char owner_last_name[NAME_LEN+1];
char breed[NAME_LEN+1];
struct dog *next;
};
struct dog *append(struct dog *list);
void search(struct dog *list);
void print(struct dog *list);
void clear(struct dog *list);
int read_line(char str[], int n);
/**********************************************************
* main: Prompts the user to enter an operation code, *
* then calls a function to perform the requested *
* action. Repeats until the user enters the *
* command 'q'. Prints an error message if the user *
* enters an illegal code. *
**********************************************************/
int main(void)
{
char code;
struct dog *dog_list = NULL;
printf("Operation Code: a for appending to the list, s for finding a dog"
", p for printing the list; q for quit. ");
for (;;) {
printf("Enter operation code: ");
scanf(" %c", &code);
while (getchar() != ' ') /* skips to end of line */
;
switch (code) {
case 'a': dog_list = append(dog_list);
break;
case 's': search(dog_list);
break;
case 'p': print(dog_list);
break;
case 'q': clear(dog_list);
return 0;
default: printf("Illegal code ");
}
printf(" ");
}
}
struct dog *append(struct dog *list){
int num;
char dogName[NAME_LEN+1];
char ownerLastName[NAME_LEN+1];
char breed1[NAME_LEN+1];
//gets user info and scans it
printf("Enter number:");
scanf("%d", &num);
//uses getchar to remove the character
getchar();
printf("enter dogs name");
fgets(dogName,NAME_LEN+1, stdin);
printf("whats the dogs breed");
fgets(breed1,NAME_LEN+1, stdin);
printf("whats the owners last name");
fgets(ownerLastName,NAME_LEN+1, stdin);
struct dog *temp = (struct dog*)malloc(sizeof(struct dog));
//if there is nothing inside list, puts num inside, and copies
//dogname, breed and so on, and puts next temp value to be null.
if (list ==NULL){
temp->number=num;
strcpy(temp->dog_name, dogName);
strcpy(temp->breed, breed1);
strcpy(temp->owner_last_name, ownerLastName);
temp->next = NULL;
return temp;
}
//if list has numbers inside then perform this
else
{
int bool = 0;
//checks if dog is already in system with using temp.
temp = list;
while (temp!= NULL){
if (temp ->number == num)
bool =1;
temp = temp->next;
}
if (bool==1){
printf("Dog is already in system!");
return list;
}
struct dog * temp1 = (struct dog*)malloc(sizeof(struct dog));
temp1->number = num;
strcpy(temp1->dog_name, dogName);
strcpy(temp1->breed, breed1);
strcpy(temp1->owner_last_name, ownerLastName);
temp1->next = NULL;
temp = list;
//while loop to go until end of list
while (temp->next !=NULL){
temp = temp->next;
}
temp->next = temp1;
return list;
}
}
void search (struct dog *list)
{
//new char name dogName so i can get from user with extra room for null temrinating
char dogName [NAME_LEN+1];
printf("Enter dog’s name please");
//stores it in
fgets(dogName, NAME_LEN+1, stdin);
//temp in order to compare and search.
struct dog *temp= list;
while (temp != NULL){
if (!strcmp(temp->dog_name, dogName)){
printf("number: %d Dog name: %s Breed: %s Owners name: %s ",
temp->number, temp->dog_name, temp->breed, temp->owner_last_name);
}
//goes on to next dog number
temp= temp->next;
}
}
void print(struct dog *list){
if (list==NULL){
printf("There is no records of that");
}
struct dog * temp = list;
//prints each record with while loop and ->next
while (temp !=NULL){
printf("number: %d Dog name: %s Breed: %s Owners name: %s ",
temp->number, temp->dog_name, temp->breed, temp->owner_last_name);
//goes on to next dog number
temp= temp->next;
}
}
void clear(struct dog *list)
{
struct dog * temp = list;
while (temp !=NULL)
{
temp= temp->next;
free(temp);
}
}
int read_line(char str[], int n)
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
}
str[i] = '';
return i;
}
The followingmust be put into standard input:
a
73
Max
Bulldog
White
a
65
Molly
Golden Retriever
Smith
a
69
Max
Poodle
Johnson
a
65
a
36
Lucy
Yorkshire
Jones
p
s
Buddy
s
Max
q
The output should look like this:
But instead im getting this.
Thank you!
opertion code a Enter patient number: 73 Enter dog name: Max Enter breed: Bulldog Enter owner last name: White Enter operation code: a Enter patient number 65 Enter dog name: Molly Enter breed Golden Retriever Enter owner last name: Smith Enter operation code: a Enter patient number: 69 Enter dog name: Max Enter breed Poodle Enter owner last name: Johnson opertion code: a Enter player number: 65 patient already existed Enter operation code: a Enter patient number 36 Enter dog name: Lucy Enter breed Yorkshire Enter owner last name: Jones Enter operation code p Number Name Breed Owner Last Name Bulldog White 73 Max 65 Molly Golden Retriever Smith Poodle 69 Max Johnson Yorkshire 36 Lucy Jones Enter operation code: f Enter dog name: Buddy Dog not found. Enter operation code: f Enter dog name: Max 73 Bulldog White Max Poodle 69 Max Johnson Enter operation code qExplanation / Answer
import java.applet.*;
import java.io.*;
import java.net.*;
import javax.sound.sampled.*;
/**
* <i>Standard audio</i>. This category provides a basic capability for
* making, reading, and saving audio.
* <p>
* The audio format uses a rate of forty four,100 (CD quality audio), 16-bit, monaural.
*
* <p>
* for added documentation, see <a href="http://introcs.cs.princeton.edu/15inout">Section one.5</a> of
* <i>Introduction to Programming in Java: AN knowledge base Approach</i> by Henry M. Robert Sedgewick and Kevin Wayne.
*/
public final category StdAudio {
/**
* The sample rate - forty four,100 cycles/second for CD quality audio.
*/
public static final int SAMPLE_RATE = 44100;
personal static final int BYTES_PER_SAMPLE = 2; // 16-bit audio
personal static final int BITS_PER_SAMPLE = 16; // 16-bit audio
personal static final double MAX_16_BIT = Short.MAX_VALUE; // thirty two,767
personal static final int SAMPLE_BUFFER_SIZE = 4096;
personal static SourceDataLine line; // to play the sound
personal static byte[] buffer; // our internal buffer
personal static int bufferSize = 0; // variety of samples presently in internal buffer
// don't instantiate
personal StdAudio()
// static initializer
static
// open up AN audio stream
personal static void init() very little Endian
AudioFormat format = new AudioFormat(SAMPLE_RATE, BITS_PER_SAMPLE, 1, true, false);
DataLine.Info data = new DataLine.Info(SourceDataLine.class, format);
line = (SourceDataLine) AudioSystem.getLine(info);
line.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE);
// the inner buffer could be a fraction of the particular buffer size, this alternative is bigoted
// it gets divided as a result of we will not expect the buffered knowledge to line up specifically with once
// the sound card decides to push its samples.
buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE/3];
} catch (Exception e)
// no sound gets created before this decision
line.start();
}
/**
* shut commonplace audio.
*/
public static void close()
/**
* Write one sample (between -1.0 and +1.0) to plain audio. If the sample
* is outside the vary, it'll be clipped.
*/
public static void play(double in) very little Endian
// send to sound card if buffer is full
if (bufferSize >= buffer.length)
}
/**
* Write AN array of samples (between -1.0 and +1.0) to plain audio. If a sample
* is outside the vary, it'll be clipped.
*/
public static void play(double[] input)
}
/**
* scan audio samples from a file (in .wav or .au format) and come them as a double array
* with values between -1.0 and +1.0.
*/
public static double[] read(String filename) {
byte[] knowledge = readByte(filename);
int N = knowledge.length;
double[] d = new double[N/2];
for (int i = 0; i < N/2; i++)
return d;
}
/**
* Play a sound file (in .wav, .mid, or .au format) during a background thread.
*/
public static void play(String filename) computer address computer address = null;
try {
File file = new File(filename);
if (file.canRead()) computer address = file.toURI().toURL();
}
catch (MalformedURLException e)
// computer address computer address = StdAudio.class.getResource(filename);
if (url == null) throw new RuntimeException("audio " + computer file name + " not found");
AudioClip clip = application.newAudioClip(url);
clip.play();
}
/**
* Loop a sound file (in .wav, .mid, or .au format) during a background thread.
*/
public static void loop(String filename) computer address computer address = null;
try {
File file = new File(filename);
if (file.canRead()) computer address = file.toURI().toURL();
}
catch (MalformedURLException e)
// computer address computer address = StdAudio.class.getResource(filename);
if (url == null) throw new RuntimeException("audio " + computer file name + " not found");
AudioClip clip = application.newAudioClip(url);
clip.loop();
}
// come knowledge as a computer memory unit array
personal static byte[] readByte(String filename) {
byte[] knowledge = null;
AudioInputStream ais = null;
try attempt to scan from file
File file = new File(filename);
if (file.exists()) {
ais = AudioSystem.getAudioInputStream(file);
knowledge = new byte[ais.available()];
ais.read(data);
}
// try and scan from computer address
else computer address computer address = StdAudio.class.getResource(filename);
ais = AudioSystem.getAudioInputStream(url);
knowledge = new byte[ais.available()];
ais.read(data);
}
}
catch (Exception e) {
System.out.println(e.getMessage());
throw new RuntimeException("Could not scan " + filename);
}
come data;
}
/**
* Save the double array as a sound file (using .wav or .au format).
*/
public static void save(String computer file name, double[] input) {
// assumes forty four,100 samples per second
// use 16-bit audio, mono, signed PCM, very little Endian
AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
byte[] knowledge = new byte[2 * input.length];
for (int i = 0; i < input.length; i++) temporary worker = (short) (input[i] * MAX_16_BIT);
data[2*i + 0] = (byte) temp;
data[2*i + 1] = (byte) (temp >> 8);
}
// currently save the file
try {
ByteArrayInputStream bais = new ByteArrayInputStream(data);
AudioInputStream ais = new AudioInputStream(bais, format, input.length);
if (filename.endsWith(".wav") || computer file name.endsWith(".WAV"))
else if (filename.endsWith(".au") || computer file name.endsWith(".AU"))
else
}
/* *********************************************************************
* sample take a look at consumer
***********************************************************************/
// produce a note (sine wave) of the given frequency (Hz), for the given
// period (seconds) scaled to the given volume (amplitude)
personal static double[] note(double cycles/second, double period, double amplitude) maths.sin(2 * scientific discipline.PI * i * cycles/second / StdAudio.SAMPLE_RATE);
return a;
}
/**
* take a look at consumer - play AN a serious scale to plain audio.
*/
public static void main(String[] args) {
// 440 cycles/second for one sec
double freq = 440.0;
for (int i = 0; i <= StdAudio.SAMPLE_RATE; i++)
// scale increments
int[] steps = zero, 2, 4, 5, 7, 9, 11, 12 };
for (int i = 0; i < steps.length; i++) {
double cycles/second = 440.0 * Math.pow(2, steps[i] / twelve.0);
StdAudio.play(note(hz, 1.0, 0.5));
}
// have to be compelled to decision this in non-interactive stuff therefore the program does not terminate
// till all the sound leaves the speaker.
StdAudio.close();
// have to be compelled to terminate a Java program with sound
System.exit(0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.