Please help in C:
with the following code, i am getting a random 127 printed in front of my reverse display of the array. The file i am pulling (data.txt) is: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
when the reverse prints, it prints: 127 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1
not sure why...please help!!
code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
void reverse(int data[MAX], int size, int i, int j, int
flip);
void display(int data[MAX], int size, int i);
int main(){
//declare pointer
FILE *myFile;
//open the file in read
myFile = fopen("data.txt", "r");
//declare variables
int data[MAX];
int i = 0;
int size = 0;
int num = 0;
int j = 0;
int flip = 0;
//check if file is available and close with print statement if
not
if (myFile == NULL){
printf("FILE NOT FOUND!!\n");
exit (0);
}
/*loop to read file data into array and
to increment size. Keeps looping until end of file*/
while(fscanf(myFile, "%d", &num)!=EOF){
data[size++] = num;
}
printf("Original Data:\n");
//call the function to print original data
display(data, size, i);
printf("\n");
printf("Flipped:\n");
reverse(data, size, i, j, flip);
printf("\n");
//close the file
fclose(myFile);
return 0;
}
//function to print original data
void display(int data[MAX], int size, int i){
printf("List =====> ");
for (i = 0; i < size; i++){
printf("%d ", data[i]);
}
printf("\n");
}
//function to flip original data and flip
void reverse(int data[MAX], int size, int i, int j, int flip){
for(i=0, j=size; i<j; i++, j--){
flip = data[i];
data[i] = data[j];
data[j] = flip;
}
printf("List =====> ");
for(i = 0; i < size; i++){
printf("%d ", data[i]);
}
}
#include <stdio.h> #include <stdlib.h> #define MAX 100 void reverse(int data[MAX], int size, int i, int j, int flip); void display(int data[MAX], int size, int i); int main() { //declare pointer FILE *myFile; //open the file in read myFile = fopen("data.txt", "r"); //declare variables int data[MAX]; int i = 0; int size = 0; int num = 0; int j = 0; int flip = 0; //check if file is available and close with print statement if not if (myFile == NULL) { printf("FILE NOT FOUND!!\n"); exit(0); } /*loop to read file data into array and to increment size. Keeps looping until end of file*/ while (fscanf(myFile, "%d", &num) != EOF) { data[size++] = num; } printf("Original Data:\n"); //call the function to print original data display(data, size, i); printf("\n"); printf("Flipped:\n"); reverse(data, size, i, j, flip); printf("\n"); //close the file fclose(myFile); return 0; } //function to print original data void display(int data[MAX], int size, int i) { printf("List =====> "); for (i = 0; i < size; i++) { printf("%d ", data[i]); } printf("\n"); } //function to flip original data and flip void reverse(int data[MAX], int size, int i, int j, int flip) { for (i = 0, j = size-1; i < j; i++, j--) { // use size-1 instead of size here flip = data[i]; data[i] = data[j]; data[j] = flip; } printf("List =====> "); for (i = 0; i < size; i++) { printf("%d ", data[i]); } }
Please help in C: with the following code, i am getting a random 127 printed in...
Please I need help in C language, I am trying to modify the code per the below instructions, but I am getting errors. Can't fgure it out. The attempted code modification and data file is privided below, after the instructions. Thank you. Instructions: 1. First, add a function named printMsg that displays a message, a greeting, or an introduction to the program for the user. Add a statement that calls that function from the main function when your program starts....
I am having problems getting the insertion sort function to output in descending order. Also, the initialize array function is not being called when I test it in the main function. I am wondering why it prints out the original array instead of initializing. Thank you. #include <stdio.h> void print_array(int *array, int length){ int i; for(i=0; i<length;i++) printf("%d"",",array[i]); printf("\n"); } void initialize_array(int *array, int length){ int i; for(i=0, i<length; i++;){ if (i%2 ==0) array[i]= 5; else array[i]= 0; } }...
Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1; FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){ numout = fread(&num, sizeof(int), 1, file); c...
The following C code keeps returning a segmentation fault! Please debug so that it compiles. Also please explain why the seg fault is happening. Thank you #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> // @Name loadMusicFile // @Brief Load the music database // 'size' is the size of the database. char** loadMusicFile(const char* fileName, int size){ FILE *myFile = fopen(fileName,"r"); // Allocate memory for each character-string pointer char** database = malloc(sizeof(char*)*size); unsigned int song=0; for(song =0; song < size;...
Missing multiple labeled functions. Card matching game in C. Shouldn't need any more functions. I am lost on how to complete the main function (play_card_match) without the sub functions complete. The program runs fine as is, but does not actually have a working turn system. It should display question marks on unflipped cards when the player is taking a turn and should also clear the screen so the player can't scroll up to cheat. Thank you #include "cardMatch.h" //main function /*...
Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix. Here is the code I have: /* * C Program to Print a Linked List in Reverse Order */ #include <stdio.h> #include <stdlib.h> struct node { int num; struct node *next; }; int main() { struct node *p = NULL; struct node_occur *head = NULL; int n; printf("Enter data into the list\n"); create(&p); printf("Displaying the nodes in the list:\n");...
Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...
Debug the following matrix program in C: // Program to read integers into a 3X3 matrix and display them #include <stdio.h> void display(int Matrix[3][3],int size); int main(void) { char size; double Matrix[size][size+1]; printf("Enter 9 elements of the matrix:\n"); int i; for (i = 0: i <= size: i++) { int j = 0; for (; j <= size++; j++){ scanf("%d", matrix[i--][4]) } } Display(Matrix,9); return 0; void...
How can I convert the following C code to MIPS Assembly? +++++++++++++++++++++++++++++++++ MIPS main program ++++++++++++++++++++++++++++++++ .data # Defines variable section of an assembly routine. array: .word x, x, x, x, x, x, x, x, x, x # Define a variable named array as a word (integer) array # with 10 unsorted integer numbers of your own. # After your program has run, the integers in this array # should be sorted. .text # Defines the start of the code...
Trying to debug a C program given to me. This is what I was given... // Program to read numeric elements (including decimals) into a 3X3 matrix and display them #include<stdio.h> int main(void) { int size = 3, Matrix[size][size]; printf("Enter 9 elements of the matrix:\n") for (int i = 0, i <=size, i++} for (int j = 0, j <= size, i++) scan("%c", Matrix1[2][2]); diplay(Matrix) float display(int Matrix1[][], int size) ( ...