Given this example :
int arr[3][7] = { {1,0,0,1,1,1,1}, //should output 4
{0,1,1,1,1,0,1}, //should output 4
{0,0,1,1,1,1,1}}; //should output 5
Find the largest sequence containing number 1, and print line index and number of 1.
Do not count total numbers of 1 in each line. Only if they are one after another.
here is my approach :
int main(){
int i,j,c=0,count=0;
int arr[3][7] = { {1,0,0,1,1,1,1}, //output 4
{0,1,1,1,1,0,1}, //output 4
{0,0,1,1,1,1,1}}; // output 5
for(i=0; i<3; i++){
for(j=0; j<7; j++){
if(arr[i][j] == 1){
c++;
} else if( arr[i][j] == 0 && c > count ) {
count = c;
c = 0;
}
}
printf("%d\n", count);
}
return 0;
}
What i want to get as output now is 4,4,5 but i am getting 1,4,5.
SOLUTION thanks to https://stackoverflow.com/users/1228887/twain249
int main(){
int i,j,c=0,count=0;
int arr[3][7] = { {1,1,0,1,1,1,1}, //output 4
{0,1,1,1,1,0,1}, //output 4
{0,0,1,1,1,1,1}}; // output 5
for(i=0; i<3; i++){
for(j=0; j<7; j++){
if(arr[i][j] == 1){
c++;
} else {
count = c;
c = 0;
}
}
if(c > count){
count = c;
}
printf("%d\n", count);
c=0;
}
return 0;
}
You forgot to handle the case where the longest sequence is the end of the list
after the inner j loop add the following
if (c > count) {
count = c;
}
Also you forgot to add a clear after each check.
After the printout add
c = clear = 0;
EDIT: 1 more error. You need to reset c even if the new sequence isn't the longest.
Change the else if into
else if (arr[i][j] == 0) { // If isn't necessary if 0/1 are your only options
{
if (c > count) {
count = c;
}
c = 0;
}
Related
A text file with names and results is put to a c program to be sorted by results. How do ignore false inserts like number 8 in the file below?
.txt code looks like this
Paul 35.6
Peter 53.4
Jack 23.0
8
Steph 0.0
Amanda 43.5
First array is arrayName[], second is arrayResult[], arrayFail[] is for 0.0 points.
#define ARRAYSIZE 15 // THIS IS THE MACRO FOR DEFINING THE ARRAY SIZE
#define STRSIZ 40 // THIS IS THE MACRO FOR DEFINING THE STRING SIZE
int main(){
/* Declaration of variables */
int i, j, n, f = 0;
float a, m =0;
char c;
/* Defining the arrays for names and results */
char arrayName[ARRAYSIZE][STRSIZ];
char arrayTemp[ARRAYSIZE][STRSIZ];
char arrayFail[ARRAYSIZE][STRSIZ];
float arrayResults[ARRAYSIZE];
n = ARRAYSIZE;
/* Code to open file and check list for validation */
FILE *fp;
fp = fopen("List.txt", "r"); //Opens the file
while ((c = fgetc(fp)) != EOF){
if (c == '\n')
m = m +1;
}
n = m;
// Close the file
fclose(fp);
/* Validates the name list */
while( n <= 0 || n > 15){
printf("\nThe list is invalid. It must be atleast one name and a maximum of 15 names. Please edit List.txt and re-run the program\n");
return 0;
}
/* Here the program scans the list from the text file */
for(i = 0; i < n; i++){
scanf("%s %f", arrayName[i], &arrayResults[i]);
}
/* Printing out the competitors list */
printf("The competitors: ");
for(i = 0; i < n - 1; i++){
printf("%s, ", arrayName[i]);
}
for(i = n - 1; i < n; i++){
printf("%s.", arrayName[i]);
}
/* The array is ordered */
for(i = 0; i < n; i++){
for(j = i + 1; j < n; j++){
if( arrayResults[i] < arrayResults[j]){
a = arrayResults[i];
strcpy(arrayTemp[i], arrayName[i]);
arrayResults[i] = arrayResults[j];
strcpy(arrayName[i], arrayName[j]);
arrayResults[j] = a;
strcpy(arrayName[j], arrayTemp[i]);
}
}
}
/* Printing out the Top 3 */
printf("\n\nThe Top 3:\n");
for(i = 0; i < 3; i++)
printf("No%d: %s with %.1f\n", i+1, arrayName[i], arrayResults[i]);
/* Failed competitors and their names are detected and stored */
for(i = 0; i < n; i++){
if( arrayResults[i] == 0 ){
strcpy(arrayFail[f], arrayName[i]);
f++;
}
}
/* Printing out the failed competitors */
printf("\nThe failed competitors were: ");
for(i = 0; i < f - 1; i++){
printf("%s, ", arrayFail[i]);
}
printf("%s.", arrayFail[f - 1]);
printf("\n");
return 0;
}
You could test the validity of each entry during the collection and bypass it when the test fails (you could adjust the test to your own goal, here we just test that the string is not empty) :
int countEntry = 0;
for(i = 0; i < n; i++){
scanf("%s %f", arrayName[i], &arrayResults[i]);
// If name or score is null, decrease i to ignore that entry
if (arrayName[i][0] == 0 || arrayResults[i][0] == 0) i--;
// Else counts the entry
else countEntry++;
}
Then you should control that there is at least one valid entry :
if(countEntry == 0) {
printf("\nThe list is invalid. It must be atleast one name. Please edit List.txt and re-run the program\n");
return 0;
}
I'm trying to make a C program to insert elements into an array until user inputs a 0 or less number, as the title says. But when I print the array out, it doesn't show the numbers I inputted. I have tried using a while as well as do-while loops but without success.
#include <stdio.h>
int main() {
int data[100];
int i;
for (i = 0; i < 100; i++) {
printf("Input your number:\n");
scanf("%d", &data[i]);
if (data[i] <= 0) {
break;
}
}
printf("Your array:");
int n = sizeof(data[i]);
for (int i = 0; i < n; i++) {
printf("%d ", &data[i]);
}
}
Try this:
#include <stdio.h>
int main() {
int data[100];
int i;
int counter = 0;
for (i = 0; i < 100; i++) {
printf("Input your number:\n");
scanf("%d", &data[i]);
counter++;
if (data[i] <= 0) {
break;
}
}
printf("Your array:");
for (int j = 0; j < counter - 1; j++) {
printf("%d ", data[j]);
}
}
The problem was that you had printf("%d ", &data[i]); instead of printf("%d ", data[i]);.
And also you've trying to get the sizeof() of an element data[i], not the size of the whole array. That's why there's counter in my code.
int n = sizeof(data[i]);
this is wrong, you want
int n = i;
sizeof(data[i]) gives you the size of an int (4 on my machine)
On the other hand, you need to check the result of scanf, if a bad input is entered do not increment the counter, something like:
int i = 0;
while (i < 100)
{
int res = scanf("%d", &data[i]);
if (res == EOF)
{
break;
}
if (res == 1)
{
if (data[i] <= 0)
{
break;
}
i++;
}
else
{
// Sanitize stdin
int c;
while ((c = getchar()) != '\n');
}
}
Finally, scanf wants a pointer to the object, but this is not the case of printf:
printf("%d ", &data[i])
should be
printf("%d ", data[i])
I am having trouble refining some code. My code takes a number "n" and calculates that many prime numbers. I need to display 10 primes per line of output data. Any tips would be appreciated.
#include <stdio.h>
int main()
{
int n, i = 3, count, c;
printf("How many primes would you like?");
scanf("%d",&n);
if ( n >= 1 )
{
printf("2");
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf(" %d",i);
count++;
}
i++;
}
return 0;
}
Just try
printf(" %5d", i);
/* ^ to help align the numbers
and
if ((count + 1) % 10 == 0)
fputc(stdout, '\n');
fix for the first time when you already print 2.
bool is_prime(int anyNum) //takes an integer array returns, is_prime
{
bool is_prime = true;
for (int c = 2; c <= anyNum - 1; c++)
{
if (anyNum % c == 0)
{
//printf("%d is not prime\r\n" , anyNum);
is_prime = false;
}
}
return is_prime;
}
int main()
{
int num_primes;
printf("How many primes would you like: ");
std::cin >> num_primes;
printf("\r\nScanned Primes Are---\r\n");
int foundPrimes = 0;
int x = 0;
for (; x <= num_primes; x++)
{
bool gotLuckyFindingPrime = is_prime( x );
if (gotLuckyFindingPrime)
{
if (foundPrimes % 10 == 0)
{
printf("\r\n");
}
printf(" %d", x);
foundPrimes = (foundPrimes + 1) % 10;
}
}
}
Does handle ten digit showing on cmd too, you can experiment with formatting
I am kind of confused. I'd like to make a program where if the number in the array has been already input, then it will detect it and say it was repeated, so the program would tell the user to put another non-repeated integer.
#include <stdio.h>
#define SIZE 5
int main()
{
int array[SIZE];
int i;
int j;
for (i = 0; i < SIZE; i++)
{
printf("[%d] Insert a number: ", i + 1);
scanf("%d", &array[i]);
j = i - 1; // This is the closest that I've gotten guys. But I need to create a loop to make j be -1 until it finds a repeated number in the array.
if (array[i] == array[j])
{
printf("The number is repeated");
i--;
}
if (array[i] > 1000)
{
printf("Sorry, the number you entered cannot be bigger than 1000\n");
i--;
}
if (array[i] < 0)
{
printf("Sorry, the number you entered cannot be less than 0\n");
i--;
}
}
for (i = 0; i < SIZE; i++)
{
printf("The array inside is %d\n", array[i]);
}
return 0;
}
As you can see, I did something similar. I just put j = i - 1 so basically it will tell the program that it was repeated. However, I suppose that I should create a loop that will subtract -1 to j until it finds the repeated value (if there is one). I just not have any idea how to create that loop and make it work.
Thank you very much!
The checks can be done the following way (without testing)
int array[SIZE];
int i;
for (i = 0; i < SIZE; i++)
{
int valid = 1;
int num;
do
{
printf("[%d] Insert a number: ", i + 1);
scanf("%d", &num );
if ( !( valid = !( num > 1000 ) ) )
{
printf("Sorry, the number you entered cannot be bigger than 1000\n");
}
else if ( !( valid = !( num < 0 ) ) )
{
printf("Sorry, the number you entered cannot be less than 0\n");
}
else
{
int j = 0;
while ( j < i && num != array[j] ) j++;
if ( !( valid = j == i ) )
{
printf("The number is repeated");
}
}
} while ( !valid );
array[i] = num;
}
This should work for you:
#include <stdio.h>
#define SIZE 5
int main() {
int array[SIZE];
int numberCount, repeatCount;
for(numberCount = 0; numberCount < SIZE; numberCount++) {
printf("[%d] Insert a number:\n>", numberCount + 1);
scanf("%d", &array[numberCount]);
for(repeatCount = 0; repeatCount < numberCount; repeatCount++) {
if (array[numberCount] == array[repeatCount]) {
printf("\nThe numbe is repeated\n");
numberCount--;
break;
}
}
if (array[numberCount] < 0) {
printf("\nSorry, the number you entered cannot be less than 0\n");
numberCount--;
}
if (array[numberCount] > 1000) {
printf("\nSorry, the number you entered cannot be bigger than 1000\n");
numberCount--;
}
}
printf("\n\n");
for(numberCount = 0; numberCount < SIZE; numberCount++)
printf("The array inside is %d\n", array[numberCount]);
return 0;
}
write a program that enter a series of integer(stores in array),then sorts the integer by calling the function selection_sort. When given an array with n elements, selection_sort must do the following:
1.search the array to find the largest,then move it to the last position.
2.call itself recursively to sort the first n-1 elements of the array.
following is my code i think the code is errors everywhere i hope some master can help me
#include <stdio.h>
int selection_sort(int a[])//this function have error that "i"and"count"is undeclared
{
int max = 0;
for (i = 1; i <= count; i++)// continuous compare to final
{
if (a[i] > max)
{
max=a[i];
}
}
a[count] = a[i]; //put the max to last position
count--;
}
int main(void)
{
int a[100] = { 0 },i=0,count=0;
while (1)
{
scanf("%d",&a[i]);
if (a[i] = '\n') { break; }//this line have error because '\n' not "int" so when i "enter" it would not break
i++;
count++; //counting how many integer i scanf
}
selection_sort();//call this function (i don't know well about function so i don't known where to put is correct )
return 0;
}
You have to compare. BUt you are assigned..
change this if (a[i] = '\n') { break; } to if (a[i] == '\n') { break; }.
You should change follows in your code.
1) change your function call..
2) declare count and i in function..
3) for taking values in to array,follow other method..
Try yourself...
Here is the complete code.Basically there are many syntax and logical error in your code. Consider this as refer and compare with your original source.
int selection_sort(int a[], int count){
int max = 0, i =0;
for (i = 0; i < count; i++){
if (a[i] > max)
{
max=a[i];
}
}
a[count] = max;
count--;
return 0;
}
int main(void) {
int a[100] = { 0 },i=0,count=0;;
printf ("Enter the total num\n");
scanf("%d",&count);
if ( ( count ) >= (sizeof(a)/sizeof(a[0])) )
return -1;
while (i < count){
scanf("%d",&a[i]);
i++;
}
selection_sort(a, count);
printf ("\nmax:%d\n", a [count]);
return 0;
}