I do not know how to pass parameters to insertSort. I am also not certain if I am using scanf correctly.
I want the dynamic array like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void insertSort(int LENGTH, int *arr)
{
int temp;
for(int i=0; i<LENGTH; i++)
{
temp = arr[i];
for(int j=i+1; j<LENGTH; j++)
{
if( temp > arr[j] )
{
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main()
{
int LENGTH = 10;
int *arr = malloc(sizeof(int)*LENGTH); //create dynamic memory
memset(arr, 0, LENGTH); //initialize the space
printf("please enter nums length : %d\n", LENGTH);
for(int i=0; i<LENGTH; i++) //put numbers to the dynamic space
{
printf("%d index : ", i);
scanf("%d", &arr[i]);
}
for(int i=0; i<LENGTH; i++) //print those space
printf("%3d", arr[i]);
insertSort(&LENGTH, &arr); //SORT !
for(int i=0; i<LENGTH; i++)
printf("%3d", arr[i]);
}
Let's take this one piece at a time. For starters, your insertSort is not an insertion sort. An insertion sort implementation looks like:
void insertSort(int LENGTH, int *arr)
{
for (int i = 1; i < LENGTH; i++)
{
int tmp = arr[i], j;
for (j = i; j >= 1 && tmp < arr[j-1]; j--)
arr[j] = arr[j-1];
arr[j] = tmp;
}
}
Next, when you are testing functions, etc.., forego the user input and just test your function with a fixed set of data as a test-case, e.g.
int main(void) {
int arr[] = { 10, 2, 8, 5, 4, 6, 7, 3, 9, 1 },
nmemb = (int)(sizeof arr/sizeof *arr);
insertSort (nmemb, arr);
for (int i = 0; i < nmemb; i++)
printf ("%3d", arr[i]);
putchar ('\n');
}
Lastly, scanf. You cannot begin to use scanf correctly unless you validate its return. Further, in your case, what happens if the user types "ten" instead of the number 10 -- try it...
Any time you are taking user-input, you must account for each character that remains in the input buffer (stdin here). This is especially true when taking input with scanf (or family) due to the way scanf handles input or matching failures. When either occurs, no further characters are read, and any offending characters are left unread in the input buffer -- just waiting to bite you again on your next attempted read (generally resulting in an infinite loop if you are taking input within a loop)
(this is one of the primary reason a line-oriented function such as fgets or POSIX getline are recommended for taking user input)
scanf can be used, if used correctly. This means you are responsible for checking the return of scanf every time. You must handle three conditions
(return == EOF) the user canceling input by generating a manual EOF by pressing Ctrl+d (or on windows Ctrl+z, but see CTRL+Z does not generate EOF in Windows 10);
(return == expected No. of conversions) indicating a successful read -- it is then up to you to check whether the input meets any additional criteria (e.g. positive integer, positive floating-point, etc..); and
otherwise, you must handle the matching or input failure and you must account for every character that may be left in your input buffer. (generally you will scan forward in the input buffer until a '\n' or EOF is found discarding any extraneous characters that remain)
If you do your job, you can successfully use scanf as needed.
Related
How can I input 2 strings which are separated by a new line?
My Problem:
First I need to give how many strings I need to get and then I need to get those strings then display it.
I tried this:
Code:
#include <stdio.h>
#include <string.h>
int main()
{
int n,i = 0;
scanf("%d", &n);
char arr[n][100];
for(int i = 0; i < n; i++)
{
scanf("%[^\n]s", arr[i]);
}
for(int i = 0; i < n; i++)
{
printf("%s\n", arr[i]);
}
return 0;
}
My Input is :
2 I am
Aravind
My Output is:
I am
þ
First Line I got correct one but second line it shows some garbage value. Help me to solve this.
You have two major problems:
The "%[" format ends with the closing "]", there should be no "s" at the end.
The "%[" format doesn't skip leading space, like that newline which will be present after the first line you read.
Both these issues can be easily solve by using fgets to read whole lines instead.
You already have suggestions to not use scanf. However, if you 'must' use scanf then you can consider the following approach:
For dynamic memory allocation you should use malloc
the newline character stays in the stdin and hence needs to be flushed or handled/ignored
Here is the updated code.
int main()
{
int n,i = 0;
scanf("%d", &n);
scanf("%*[\n]");
/*this will read the \n in stdin and not store it anywhere. So the next call to
* scanf will not be interfered with */
char **inputs;
inputs = malloc(n * sizeof(char *));
for (i = 0; i < n; i++)
{
inputs[i] = malloc(100 * sizeof(char));
}
for(i = 0; i < n; i++)
{
scanf("%*[\n]");
scanf("%100[^\n]", inputs[i]);
}
for(i = 0; i < n; i++)
{
printf("%s\n", inputs[i]);
}
return 0;
}
use gets(arr[i]) instead of scanf.
I'm fairly new to coding and am currently taking a programming course at school with C. We were given an assignment and I'm having a bit of difficulty with the first part. We're learning how to use the string-handling library (stdlib.h) and the objective of the assignment is to input multiple lines of text from the keyboard. The instructor advised us to use two-dimensional arrays in order to do this, but I'm a bit stuck. Here's the code I've written:
int main(void) {
char string[3][SIZE];
int i, j;
int c;
printf("Enter three lines of text:\n");
for (i = 0; i < 3; i++) {
j = 0;
while ((j < SIZE) && (c = getchar() != '\n')) {
string[i][j] = c;
j++;
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < SIZE; j++) {
printf("%c", string[i][j]);
}
printf("\n");
}
return 0;
}
Some points that I'd like to make are that I used the getchar() function to receive input one character at a time, and also the second for loop I intended to print each line of text that is stored in each row of the string array.
The input is any string of text for three lines, for example:
Hi my name is John.\n
I am from the US\n
and I'm a student.
Here's what the current output looks like:
Enter three lines of text:
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr...
The output that I'm expecting is:
Enter three lines of text:\n
Hi my name is John.\n
I'm from the US\n
and am a student.
Any tips or advice would be greatly appreciated. Thank you!
First of all let me commend the fact the you starting your way with C. That's the most solid language to learn(better is only assembly itself) - you will have a full understanding of how things work, which you wouldn't get if started from some language written on top of C(like Java and Python).
But it's a hard and long road, which worth that.
On the code: there is a lot going and you have made a lot of amusing bugs that would reproduce different interesting things every other time and machine you run it.
First of all: to make your code work somehow all you need is add parenthesis:
while ((j < SIZE) && ((c = getchar()) != '\n')) {
In C everything is binary(or integer, depending how you look at it) and default binding is to the right a op1 b op2 c op3 d..
First op3 is evaluated c op3 d = r1, then you have a op1 b op2 r1 and so on.
Thus you was comparing the value of getchar() with value of character '\n' - which are not equal, so you get TRUE (value 1) and store it in local variable c.
Next you still have some problems because of the way you initialized your array:
char string[3][SIZE];
What it does is simply "intrusts" 3*SIZE*sizeof(char) bytes of you process address space to a thing labeled "string". But that does not clear up all the remnants of previous live (of your program, or even before) on those bytes, so if it happens that SIZE in your program == 100 and you used to store your credit card on a real address memory (RAM) mapped to that region of your program memory you would see your credit card when you print it by printf - if you didn't overwrite those 300 bytes.
This may help you looking at it:
#include <stdlib.h>
#include <stdio.h>
#define SIZE 10
int main(void) {
char string[3][SIZE];
int i, j;
int c;
for(i = 0; i < 3; i++)
for(j = 0; j < SIZE; j++){
string[i][j] = 0;
}
printf("Enter three lines of text:\n");
for (i = 0; i < 3; i++) {
j = 0;
while ((j < SIZE) && ((c = getchar()) != '\n')) {
string[i][j] = c;
j++;
}
}
for (i = 0; i < 3; i++) {
for (j = 0; j < SIZE; j++) {
printf("%c", string[i][j]);
}
printf("\n");
}
return 0;
}
Also be aware that getchar() may behave lousy with input and newlines - it depends on whether you console buffers input before sending it to your program on enter(newline) or not. More here How to avoid press enter with any getchar()
Note: I wrote this answer before the OP clarified they had to use getchar.
To read a whole line at a time, use fgets. To print a whole string at a time, use printf with the %s format.
#include <stdio.h>
int main(void) {
// No need to define a SIZE constant.
// Because it's stack allocated we can its size.
char strings[3][100];
printf("Enter three lines of text:\n");
for ( int i = 0; i < 3; i++) {
// Reads one line, up to the size of `strings[i]`, from stdin.
fgets( strings[i], sizeof(strings[i]), stdin );
}
for ( int i = 0; i < 3; i++) {
// Print each string and its line number.
printf("Line %d: %s\n", i, strings[i]);
}
return 0;
}
This is not the best pattern to read input. You'll learn very quickly that fixed memory sizes and reading input don't work well. For future reference, it would be more like this.
#include <stdio.h>
#include <string.h>
int main(void) {
// A list to store 3 strings, but no memory for the strings themselves.
char *strings[3];
printf("Enter three lines of text:\n");
// A line buffer that's sufficiently large.
// This will be reused.
char line[4096];
for ( int i = 0; i < 3; i++) {
// Read into the large line buffer.
fgets( line, sizeof(line), stdin );
// Copy the string into a buffer that's just big enough.
strings[i] = strdup( line );
}
for ( int i = 0; i < 3; i++) {
printf("Line %d: %s\n", i, strings[i]);
}
return 0;
}
This allocates a single large line buffer to do the reading, then copies what its read with strdup to memory of just the right size. This lets you read even very long lines of input without wasting a bunch of memory if they're very short.
Note that strdup() is not part of the C standard library, but it's part of the POSIX spec. Any major compiler will have it, and it's easy to write your own.
I am taking a couple of numbers as input from the user, storing them into a file and then reading from that file, sorting the numbers and displaying them to the user. The program works except for 1 issue. When taking input from the user it asks for an extra elememnt than the specifies limit. This extra element doesn't get stored anywhere. I have tried reducing the limit value by one but that results in loss of 1 element(i.e. 1 element becomes 0). I understand that this may be a very newbie issue but I coudn't find any direct answer to this issue. I wrote the program in Visual Studio 2010 as a C program. here's the code:
#include <stdio.h>
int arr[50];
int n;
int writefile()
{
FILE *ptr;
ptr = fopen("Sort.txt","w");
if(ptr==NULL)
{
printf("No such file exists\n");
fclose(ptr);
return 0;
}
else
{
int i;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for(i=0; i<n; i++) //The issue is here
{
scanf("%d\n",&arr[i]); //say the user enter n=3, then after adding 1,2,3 it will ask for another 4th element but only 1,2,3 will get stored.
}
fwrite(arr, sizeof(int), n, ptr);
fclose(ptr);
printf("done\n");
}
}
void sortarr(int arr1[]);
int readfile()
{
FILE *ptr;
ptr = fopen("Sort.txt","r");
if(ptr==NULL)
{
printf("No such file exists\n");
fclose(ptr);
return 0;
}
else
{
int i;
int arrb[50];
fread(arrb, sizeof(int), n, ptr);
printf("Before sorting data\n");
for(i=0; i<n; i++)
{
printf("%d",arrb[i]);
}
printf("\n");
sortarr(arrb);
printf("After Sorting data\n");
for(i=0; i<n; i++)
{
printf("%d",arrb[i]);
}
printf("\n");
}
}
void sortarr(int arr1[])
{
int i,j,temp;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (arr1[i] > arr1[j]) {
temp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = temp;
}
}
}
}
int main()
{
writefile();
readfile();
return 0;
}
scanf("%d\n",&arr[i]);
Should be:
scanf("%d",&arr[i]);
If you ask it to ignore white space after the number, it will have to keep reading until it reads some non-whitespace in order for it to ensure it has ignored all the whitespace. You definitely don't want scanf to try to ignore whitespace after it has read the number, you want it to terminate.
The %d format specifier already ignores whitespace before the number.
Let's just narrow this down to the following code:
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d\n", &arr[i]);
}
Notice the difference in the scanf calls? The first call doesn't have a \n in its format string. The second call does.
Remove the \n from the scanf format string (in the second call). Things should work more like you're expecting then.
An online manual page for scanf says:
The format string consists of a sequence of directives which describe how to process the sequence of input characters... A directive is one of the following: A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input.
So the looped over scanf (with the extra \n in it) was effectively reading the number, the newline, the next number, then realizing it's read all of the white space it could. scanf then returned with the first number assigned to your array entry and the next number ready for the next call to reading from standard input. This left things effectively offset appearing like you saw and requiring the extra number before ending (in order that it could detect that the white space had ended).
I am trying to compare strings in a mergesort algorithm after reading characters from a file as integers, and converting them to strings in an array. I am able to get the strings to print out, but when the char[] array is passed to the mergesort algorithm, the program crashes at the strcmp() step which is in the merge() step of the merge sort.
I tested to see that my temp char[] arrays are not initializing properly, so I think the problem is that I am not passing the original char[] array "charr" to the mergsort function.
I am lost as to how to do this. I borrowed the mergesort algorithm from the web, and it works on int arrays, but the simple change of changing int[] arrays to char[] arrays does not work.
How is it possible to get the char[] array that I want the sort done on to pass properly and initialize in the mergesort function?
The permutations look like this in the text file:
aaaab
aaaba
aabaa
abaaa
baaaa
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void) {
int arr[243][6];
//This is the array that I want to store my strings
char *charr[243][6];
int c, i = 0 , j = 0;
FILE *file;
file = fopen("permutations.txt", "r");
if (file) {
while ((c = getc(file)) != EOF) {
// we are reading each char in the string
//every time we hit a new line char (\n = 10)
//advance the array one, otherwise add the
// char
if (c != 10) {
arr[i][j] = c;
j++;
}
else {
arr[i][j] = c;
sprintf(charr[i], "%d%d%d%d%d%d", arr[i][0], arr[i][1],
arr[i][2], arr[i][3], arr[i][4]);
i++;
j = 0;
}
}
fclose(file);
}
if (strcmp(charr[0],charr[1]) < 0)
printf("less\n");
else
printf("other\n");
r_mergesort(charr,0,242);
for (int k = 0; k < 243; k++) {
printf(charr[k]);
for (int l = 0; l < 6; l++) {
putchar(arr[k][l]);
}
}
return 0;
}
/*l is for left index and r is right index of the sub-array*/
void r_mergesort (char arr[], int l, int r) {
//base case
if (l < r) {
//divide
int m = (l + r) /2;
// recursively sort halves
r_mergesort(arr, l, m);
r_mergesort(arr, m + 1, r);
// merge results
merge(arr, l, m, r);
}
}
void merge (char arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
// create temp arrays
char left[n1], right[n2];
// copy data to temp arrays
for (i = 0; i < n1; i++) {
left[i] = arr[l + i];
}
for (j = 0; j < n2; j++)
right[j] = arr[m + 1 + j];
// merge the temp arrays back into arr[]
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (strcmp(left[i], right[j]) < 0) {
arr[k] = left[i];
i++;
}
else {
arr[k] = right[j];
j++;
}
k++;
}
//copy the remaining elements of left[]
while (i < n1) {
arr[k] = left[i];
i++;
k++;
}
//copy the remaining elements of right[]
while (i < n2) {
arr[k] = right[j];
j++;
k++;
}
}
While there is nothing wrong with character-oriented input (e.g. getc) if, as you describe, your permutations.txt contains one possible permutation per-line, then using line-oriented input will simplify your read (which I suspect is where a bulk of your problem lies). So let's get you reading your data file properly as a start to solving your issues.
Using line-oriented input, your primary functions are fgets and getline. Each has certain pluses and minuses. Since you are dealing exclusively with static declarations, we will use fgets below for the example.
One thing to be aware of with line-oriented input, is that fgets will read until a newline ('\n') is encounter or the maximum number of characters specified (minus 1 leaving room for the nul-terminator). What this means in your case is, if you have declared charr[243][7] and have 6 chars per-line (plus the '\n' for a total of 7 chars) you will run into problems if you do not increase your string size by an additional character to allow the '\n' to be read as part of each line (and also providing space for the nul-terminator).
Basically what will happen is you will tell fgets to read a max of 7 chars which means it will read all 6 of your permutation characters, but leave the '\n' at the end of the line unread. Your next call to fgets will read only the '\n'. To solve the entire problem, simply declare charr[243][8] = {{0}}; to allow a complete read of each line.
You may say, 'that doesn't sound much simpler' -- it is, I just wanted to make sure and give a thorough explanation so you don't end up caught in a subtle issue of reading 1-less that the whole line. Of course, since all line-oriented input functions, read and include the '\n' as part of their read, you will want to remove the newline from the stings stored in the array. After the explanation, hopefully the example will make the read much clearer:
#include <stdio.h>
#include <string.h>
#define MAXR 243
#define MAXC 8
int main (int argc, char **argv) {
char charr[MAXR][MAXC] = {{0}};
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
size_t i = 0;
if (!fp) {
fprintf (stderr, "error: file open failed '%s'\n", argv[1]);
return 1;
}
while (i < MAXR && fgets (charr[i], MAXC, fp))
{
/* get length, strip trailing newline */
size_t len = strlen (charr[i]);
if (charr[i][len-1] == '\n') charr[i][len-1] = 0;
printf (" charr[%zu] : %s\n", i, charr[i]);
i++;
}
if (fp != stdin) fclose (fp);
return 0;
}
The code above simply reads and prints (with the line index) each permutation read from the file given as the first argument to the program (or from stdin if no filename is given). It is just to confirm the read of your permutations.txt file to begin with.
Compile
gcc -Wall -Wextra -O3 -o bin/readperm readperm.c
Test Input (permutations.txt)
$ cat permutations.txt
123456
234561
345612
456123
Output
$ ./bin/readperm permutations.txt
charr[0] : 123456
charr[1] : 234561
charr[2] : 345612
charr[3] : 456123
While fgets and getline are the primary tools for line-oriented input, and I rarely recommend the scanf family of functions, if your permutations.txt file is exactly as you describe fscanf can be used very efficiently in this case. Generally, the choice of format-string and proper format-specifiers is what gives new C programmers fits. Since fscanf doesn't require that you read the newline, you can use the char charr[243][7] = {{0}}; declaration and not have to worry about removing the included newline. Specifically, you can replace the read loop above with:
while (i < MAXR && fscanf (fp, " %[^\n]%*c", charr[i]) == 1)
{
printf (" charr[%zu] : %s\n", i, charr[i]);
i++;
}
Note the choice of format specifier " %[^\n]%*c". The leading space between the opening " and '%' will skip any whitespace before the first character. The character case expression used as a format-specifier %[^\n] will read all characters up to, but not inlcuding, the newline. The assignment-suppression %*c will read and discard the '\n' without including it in your string (or in the match-count returned by fscanf).
note you could simply use a " %s" format specifier and accomplish the same read in your case, but that would have eliminated the explanation of the various parts of the format-string that is critical to understanding correct use of the scanf family of functions.
Lastly, note above the use of the return == 1. fscanf returns the number of successful conversions (according to the format-specifiers). Therefore you want to continue your read so long as fscanf makes a single conversion to string each time it is called. When it fails to make a proper conversion your read loop terminates (you can assign the return to a variable and check within the body of the loop to confirm EOF versus a read error)
Let me know when you get your read of permutations.txt sorted out and we will work on any lingering issues you have after confirming you have your read fixed.
try char *charr[243][6]; --> char charr[243][7];
char arr[] --> char arr[][7] The modified program you consider it
sprintf(charr[i], "%d%d%d%d%d%d" --> sprintf(charr[i], "%c%c%c%c%c%c"
– BLUEPIXY
The three changes you suggested were the ones that got me on the right track. I had to initialize then entire length of each string in the merge algorithm, it was not enough to say left[i] = arr[l + i].
– lefunction
My task is read two strings of digits and save them in different arrays.
I decided to use scanf function, but program can read only first string.
This is my bad-code.
int main()
{
int firstArray[50], secondArray[50], i, j;
/* fill an array with 0 */
for(i=0; i<50; ++i)
{
firstArray[i]=secondArray[i]=0;
}
i=j=0;
while((scanf("%d", &firstArray[i]))== 1) { ++i; }
while((scanf("%d", &secondArray[j]))== 1) { ++j; }
/* Print this. */
for(i = 0; i < 20; ++i)
{
printf("%d ", firstArray[i]);
}
putchar('\n');
for(j = 0; j < 20; ++j)
{
printf("%d ", secondArray[j]);
}
return 0;
}
I just don't understand how scanf function works. Can someone please explain?
scanf ignores blank characters (including new line). Thus your scan will read entire input into firstArray if you have no "non blank" separator.
If file/data has ; at end of first line it will stop the read into firstArray there, and never read anything into secondArray - as you never consume the ;.
/* This will never be 1 as ; is blocking */
while((scanf("%d", &secondArray[i])) == 1) {
So: if you separate with i.e. ; you will have to read / check for this before you read into secondArray.
You could also add something like:
char c;
/* this can be done more tidy, but only as concept */
while((scanf("%d", &firstArray[i])) == 1 && i < max) {
++i;
if ((c = getchar()) == '\n' || c == ';')
break;
}
Also instead of initializing array to 0 by loop you can say:
int firstArray[50] = {0}; /* This set every item to 0 */
Also take notice to ensure you do not go over your 50 limit.
You say strings of digits and you read %d. The format scans the input for the longest sequence representing an integer (signed) value. Two "digit strings" are consumed by the first while loop.
EDIT Instead of "strings of digits" you should say "strings of integers". In this case it is a little bit more subtle since the first while can consume all the integers, unless they are separated by something that is not a possible integer (e.g. a ;).
So, to make the following to work, you must separate the two "lines" with something that can't be parsed as integer and which is not considered "white character". Not the better solution, but one the possible.
#include <stdio.h>
#include <ctype.h>
int main()
{
int firstArray[50] = {0};
int secondArray[50] = {0};
int i, j, l1, l2;
int tmp;
i = j = 0;
// read integers, but not more than size of array
while( scanf("%d", &firstArray[i]) == 1 && i < sizeof(firstArray) ) {
++i;
}
// consume non digits
for(tmp = getchar(); tmp != EOF && !isdigit(tmp); tmp = getchar());
// on EOF you should exit and stop processing;
// we read one more char, push it back if it was a digit
if (isdigit(tmp)) ungetc(tmp, stdin);
while( scanf("%d", &secondArray[j]) == 1 && j < sizeof(secondArray) ) {
++j;
}
l1 = i; // preserve how many ints were read
l2 = j;
/* Print this. */
for(i = 0; i < l1; ++i)
{
printf("%d ", firstArray[i]);
}
putchar('\n');
for(j=0; j < l2; ++j)
{
printf("%d ", secondArray[j]);
}
return 0;
}
EDIT A solution that maybe fits your need better is to read the lines (one per time) into a buffer and sscanf the buffer.
You cannot use scanf to do that.
Read the documentation.
Observations:
with scanf if you enter a digit your loop runs forever
there is no check on size 50 limit of your arrays
if you press return then it ignores that line because does not match your pattern
if you enter a letter the pattern does not match and loop breaks
So use some other function, maybe gets, atoi or strtol. And remember to check the size 50 limit of your arrays.
Actually, there is one special point in C's arrays.
Though you declare an array's size. say int arr[5]; You can store values beyond the size of 5. It doesn't show any error but leads to undefined behavior (Might overwrite other variables).
Please Refer this question: Array size less than the no. of elements stored in it
In you case, that was your problem. The compiler had never passed beyond the first while statements. Thus, you didn't get any output. In fact, it didn't even compile the whole code yet!
while((scanf("%d", &firstArray[i]))== 1) { ++i; }
So, you could write this while statement like this:
while( scanf("%d", &firstArray[i]) ==1 && i<50 )
i++;
or else:
while(i<50 )
{
scanf("%d", &firstArray[i]);
i++;
}
or else:
for (i=0; i<50; i++)
scanf("%d", &firstArray[i]);