finding the odd numbers from below programs divisors [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
#include <iostream>
using namespace std;
int main()
{ int i,num1,num2,sum=0,count=0;
cout<<"Enter a range: ";
cin>> num1>> num2;
for(i = num1;i <= num2; i++)
if(i%3 ==0 ||i%5 ==0)
{
count++;
//sum=sum+i;
cout<<i<<" ";
}
return 0;
}
I did a program of finding the divisors of 3 and 5 in a given range, but now i want to find the odd numbers from that divisors.how to do that???
suppose for this program i enter the range 1 to 20.And i will get divisors:3,5,6,10,12,15,18,20.Now i want to get odd numbers from this numbers.How to do that??

If you want to check if i is odd, simply add i % 2!= 0 into the condition.
Also as leaf pointed out, don't use using namespace std; and while you're there, why not actually use the variable count?
#include <iostream>
int main(){
int low{ 0 };
int max{ 0 };
std::cout << "Type in low: ";
std::cin >> low;
std::cout << "Type in max: ";
std::cin >> max;
unsigned int count{ 0u };
for (int i = low; i < max; ++i){
if (i % 2 != 0 && (i % 3 == 0 || i % 5 == 0)){
++count;
std::cout << i << " ";
}
}
std::cout << "\nNumber of odd integers that are multiples of 3 or 5 found: " << count << std::endl;
return 0;
}
Example run:
Type in low: 300
Type in max: 795
303 305 309 315 321 325 327 333 335 339 345 351 355 357 363 365 369 375 381 385
387 393 395 399 405 411 415 417 423 425 429 435 441 445 447 453 455 459 465 471
475 477 483 485 489 495 501 505 507 513 515 519 525 531 535 537 543 545 549 555
561 565 567 573 575 579 585 591 595 597 603 605 609 615 621 625 627 633 635 639
645 651 655 657 663 665 669 675 681 685 687 693 695 699 705 711 715 717 723 725
729 735 741 745 747 753 755 759 765 771 775 777 783 785 789
Number of odd integers that are multiples of 3 or 5 found: 115

Related

Segmentation faults and erroneous output from std::sort

There is something very wrong with the following code, but I can't figure out what it is.
#include <map>
#include <iostream>
#include <random>
#include <algorithm>
class A
{
public:
A(int v) : val(v) { }
A() = delete;
const size_t val;
};
typedef std::shared_ptr<A> A_ptr;
class B
{
public:
B(unsigned int L)
{
std::mt19937 rand_engine(9341255);
std::uniform_int_distribution<int> dist(1,1000);
for (int i = 0; i < L; ++i)
{
m.insert(std::pair<unsigned int, A_ptr>(i, std::make_shared<A>(dist(rand_engine))));
}
}
B() = delete;
std::map<unsigned int, A_ptr> m;
};
int main()
{
unsigned int b_size = 47;
B b(b_size);
std::vector<A_ptr> v = {};
for (std::map<unsigned int, A_ptr>::iterator it = b.m.begin(); it != b.m.end(); ++it)
{
v.push_back(it->second);
}
std::cout << "v = ";
for (auto & i : v)
std::cout << i->val << " ";
std::cout << "\n";
std::sort(v.begin(), v.end(), [](A_ptr a1, A_ptr a2){ return (a1->val >= a2->val); });
std::cout << "v sorted = ";
for (auto & i : v)
std::cout << i->val << " ";
std::cout << "\n";
return 0;
}
This code compiles and executes, and yields the following output:
g++ -std=c++17 map_sort.cpp -o map_sort && ./map_sort
v = 634 739 51 906 227 185 738 302 310 888 886 644 191 719 68 212 124 732 879 724 671 167 367 313 813 1000 905 24 245 320 580 605 641 760 23 382 348 718 373 937 733 335 306 679 840 880 138
v sorted = 1000 937 906 905 880 888 886 245 879 840 813 760 733 719 718 644 641 739 738 732 724 679 634 671 605 580 382 373 367 320 348 335 313 310 306 302 227 68 191 185 167 212 138 124 51 24 23
Note that the output on the v sorted line is NOT correctly sorted.
Changing unsigned int b_size = 47 to unsigned int b_size = 48 yields the following output:
g++ -std=c++17 map_sort.cpp -o map_sort && ./map_sort
v = 634 739 51 906 227 185 738 302 310 888 886 644 191 719 68 212 124 732 879 724 671 167 367 313 813 1000 905 24 245 320 580 605 641 760 23 382 348 718 373 937 733 335 306 679 840 880 138 924
Segmentation fault: 11
Similar behaviors occur for other values of b_size as well. It wouldn't surprise me if they're coming from the same underlying problem. I'm clearly using std::sort incorrectly, but I have no idea how. Can anybody shed some light on this?

Why does it give a "multiple test case" error?

I've written the code for eliminating the largest 2 elements of an array, but this code gives junk value for testcase > 1. Why?
Input:
no of TestCase
size of array
elements of array
Sorting function:
int sort_asc(int arr[], int n)
{
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(arr[j]<arr[i])
{
int temp;
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
int main() {
//code
int test;
cin>>test;
while(test--){
//taking size and array as inputs
int size;
cin>>size;
int a[size];
cin>>a[size];
for(int i=0;i<size;i++){
cin>>a[i];
}
//sorting the array
sort_asc(a,size);
//printing the output discarding last 2 elements of the array
for(int i=0;i<size-2;i++){
cout<<a[i]<<" ";
}
cout<<"\n";
}
return 0;
}
Expected:
12 23 28 43 44 59 60 68 70 85 88 92 124 125 136 168 171 173 179 199 212
230 277 282 306 314 316 325 328 336 337 363 365 368 369 371 374 387 394 414
422 427 430 435 457 493 506 527 531 538 541 546 568 583 650 691 730 737 751
764 778 783 785 789 794 803 809 815 847 858 863 874 887 896 916 920 926 927 930 957
My output:
12 23 28 43 44 59 60 68 70 81 85 88 92 124 125 136 168 171 173 179 199 212 230 277 282 306 314 316 325 328 336 337 363 365 368 369 371 374 387 394 414 422 427 430 435 457 493 506 527 531 538 541 546 568 583 650 691 730 737 751 764 778 783 785 789 794 803 809 815 847 858 863 874 887 896 916 920 926 930 957
A VLA (variable length array) is invalid C++ code. It is tolerated by some compilers, but it is still invalid.
But that is not your main problem. You produced an out of bound error. An array index starts with 0. The last element is at position size-1. So your statement
cin>>a[size];
will write past the end of your array. Producing undefined behavior.
I am not sure, why you put the statement at all, but after that, anything undefined can and most probably will happen.

Primes with argc and argv

I have an assignment in Codejudge which I write a command line program which reads a space separated list of integers from the command line and prints the ordered sublist consisting of the input prime numbers.
I tried numerous times but I can't seem to work
this is input argument:
9308 2034 9466 283 7949 1153 7241 5341 4693 6910 6852 5540 8015 9305 5697 1395 4727 9159 8661 1367 6096 2911 4797 8025 2593 5460 5767 5543 2429 8371 6024 2343 285 8657 9869 5388 5295 6279 3084 9573 6980 2362 1565 5134 5185 1991 7142 3699 5937 4151 3044 2468 8005 1603 662 2989 752 6971 3152 3681 9743 653 4542 719 2081 5772 9179 4034 5904 5494 1653 251 130 6646 2835 2260 8998 7464 112 2179 6592 8502 7381 5990 6681 8237 1331 537 2048 3342 9353 7883 1041 621 1022 4569 1421 9592 877 657 7097 2828 6242 2216 387 4605 8017 2784 4509 5818 7959 1612 491 6381 6530 5773 2220 2802 6478 7401 9084 1845 8805 8192 9806 6940 6578 9132 3144 8793 4854 1087 3238 8622 419 346 2598 1194 5766 4626 4740 6191 8639 7948 9833 3117 232 5839 8726 4863 4532 3498 6717 4874 3496 2951 5750 6982 1779 9614 9519 5980 3245 2698 6771
etc.
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
int main(int argc, char* argv[]) {
std::vector<int> input;
std::vector<int> output;
for (int a = 0; a < argc; a++) {
input.push_back(std::atoi(argv[a]));
}
int count = 0;
for (int i = 0; i < input.size(); i++) {
if (input.at(i) % 2 != 0 && (input.at(i) % 3 != 0 || input.at(i) / 3 == 1) && (input.at(i) % 5 != 0 || input.at(i) / 5 == 1) /*&& input.at(i)*input.at(i)% input.at(i)!=0*/) {
output.push_back(input.at(i));
count++;
}
}
sort(output.begin(), output.end());
for (int i = 0; i < count; i++) {
std::cout << output[i] << " ";
}
}
expected result:
1 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241
actual result:
1 3 5 7 11 13 17 19 23 29 31 37 41 43 47 49 53 59 61 67 71 73 77 79 83 89 91 97 101 103 107 109 113 119 121 127 131 133 137 139 143 149 151 157 161 163 167 169 173 179 181 187 191 193 197 199 203 209 211 217 221 223 227 229 233 239 241
there are difference between the expected and the actual.
keep in mind that the vector of random numbers are in random orders and not from smallest to largest and they all are
for (int a = 0; a < argc; a++) {
input.push_back(std::atoi(argv[a]));
}
should be
for (int a = 1; a < argc; a++) {
input.push_back(std::atoi(argv[a]));
}
The first argument argv[0] is the program name.

Insertion Array Sorting Method

I am currently a student working on an insertion sort method.
Below is the code:
//Insertion Sorting of an Integer array
void InsertionSort(int insertVals[]){
//Systematic processing of the Array
for(int i = 0; i < INITSIZE - 1; i++){
//Value to check
int temp = insertVals[i];
//Index placeholder for the insterion sort
int k;
//Shifts the int array
for(k = i; k > 0 && insertVals[k-1] > temp; k--){
insertVals[k] = insertVals[k-1];
}
//Inserts the checked value back into the array
insertVals[k] = temp;
}
}
In my tests, I have given it the array from left to right:
307 249 73 158 430 272 44 378 423 209
440 165 492 42 487 3 327 229 340 112
303 169 209 157 60 433 99 278 316 335
97 326 12 267 310 133 479 149 79 321
467 172 393 336 485 245 228 91 194 357
1 153 208 444 168 490 124 196 30 403
222 166 49 24 301 353 477 408 228 433
298 481 135 13 365 314 63 36 425 169
115 94 129 1 17 195 105 404 451 298
188 123 5 382 252 66 216 337 438 144
The method produces from left to right:
314 63 314 63 36 425 36 169 425 169
115 115 94 129 94 129 1 17 195 105
404 451 298 188 123 5 382 252 66 216
337 438 144 1 17 195 105 404 451 298
188 123 5 382 252 66 216 337 438 144
228 229 245 249 252 267 272 278 298 298
301 303 307 310 314 316 321 326 327 335
336 337 340 353 357 365 378 382 393 403
404 408 423 425 430 433 433 438 440 444
451 467 477 479 481 485 487 490 492 144
What am I incorrectly coding?
Thanks!
EDIT:
//In main...
Printing(insertionSortValues, "Insertion Sorted Array");
//Function for Print
void Printing(int vals[], string s){
cout << s << ":" << endl;
for(int i = 0; i < INITSIZE; i++){
if(i % 10 == 0){
cout << endl;
}
cout << setw(3) << vals[i] << " ";
}
cout << endl;
}
The solution to this problem was solved through #PaulMcKenzie.
The line:
for(int i = 0; i < INITSIZE - 1; i++){
needed to become:
for(int i = 0; i <= INITSIZE - 1; i++){
Below is the corrected function.
//Insertion Sorting of an Integer array
void InsertionSort(int insertVals[]){
//Systematic processing of the Array
for(int i = 0; i <= INITSIZE - 1; i++){
//Value to check
int temp = insertVals[i];
//Index placeholder for the insterion sort
int k;
//Shifts the int array
for(k = i; k > 0 && insertVals[k-1] > temp; k--){
insertVals[k] = insertVals[k-1];
}
//Inserts the checked value back into the array
insertVals[k] = temp;
}
}

Why is it counting the last number twice?

My program is reading numbers from a file- and it reads the last number twice. What is wrong with my program and what can I do to fix it?
int main()
{
ifstream inputFile;
int number = 0; //the input for the numbers in the file
double total = 0; //the total of the numbers
double counter = 0;//number of numbers
double average = 0;//average of the number
inputFile.open("random.txt");//open file
if (!inputFile)//test for file open errors
{
cout<<"Error \n";
}
while (inputFile)
{
inputFile >> number ;
total+=number;
counter++;
cout<<"The running total is: " <<total <<"\n";
}
total=total*1.00;
counter=counter*1.00;
average = total/counter;
cout<<"\nthe final total is: \t" <<total;
cout<<"\nthe number of numbers is: \t";
cout<<counter;
cout<<"\nthe average of the numbers is: \t";
cout<<setprecision(8)<< fixed<< average<<"\n";
inputFile.close();
return 0;
}
the contents of the file:
42
468
335
501
170
725
479
359
963
465
706
146
282
828
962
492
996
943
828
437
392
605
903
154
293
383
422
717
719
896
448
727
772
539
870
913
668
300
36
895
704
812
323
334
674
665
142
712
254
869
548
645
663
758
38
860
724
742
530
779
317
36
191
843
289
107
41
943
265
649
447
806
891
730
371
351
7
102
394
549
630
624
85
955
757
841
967
377
932
309
945
440
627
324
538
539
119
83
930
542
834
116
640
659
705
931
978
307
674
387
22
746
925
73
271
830
778
574
98
513
987
291
162
637
356
768
656
575
32
53
351
151
942
725
967
431
108
192
8
338
458
288
754
384
946
910
210
759
222
589
423
947
507
31
414
169
901
592
763
656
411
360
625
538
549
484
596
42
603
351
292
837
375
21
597
22
349
200
669
485
282
735
54
1000
419
939
901
789
128
468
729
894
649
484
808
422
311
618
814
515
Because the inputFile becomes false1 after an unsuccessful reading attempt has been done, and not when there's just no more data to read. So, when you've read successfully the last element you are inputFile still evaluates to true, and the next iteration of the while is started. Now, at inputFile>>number the failbit is set, but you're not checking it immediately, so your code goes on normally, "thinking" that another element has been read (when actually is just the old one which happened to remain in number).
Quick solution: move the check after the read:
for(;;)
{
inputFile >> number;
if(!inputFile)
break;
total+=number;
counter++;
cout<<"The running total is: " <<total <<"\n";
}
or (better):
while(inputFile >> number)
{
total+=number;
counter++;
cout<<"The running total is: " <<total <<"\n";
}
This works because operator>> returns the stream object, which is evaluated just after the read in the while condition section.
1. I know, I know that it's not actually false but it's operator(void*)... but don't overcomplicate things :)
This line of code fails when it reaches the end of the file:
inputFile >> number;
but your code doesn't check to see if the stream is still valid until the beginning of the while loop.
Try:
while(inputFile >> number)
{
total+=number;
counter++;
cout<<"The running total is: " <<total <<"\n";
}
I suspect that the boolean test on inputfile (in the while-line) only fails if the eof has actually been read. So the loop will be executed one time too much and number will still have its old value, and therefore be added twice.
See http://www.cppreference.com/wiki/io/eof for an example on how to write a better while loop for reading files. You could do it like this:
while (inputFile >> number)
{
total+=number;
counter++;
cout<<"The running total is: " <<total <<"\n";
}
You've been given one approach. Here's another possibility:
int main() {
std::ifstream inputFile("random.txt");
std::vector<int> data;
std::copy(std::istream_iterator<int>(inputFile),
std::istream_iterator<int>(),
std::back_inserter(data));
double total = (double)std::accumulate(data.begin(), data.end(), 0);
std::cout << "The final total is: " << total << "\n";
std::cout << "The number of numbers is: " << data.size() << "\n";
std::cout << "The average is: " << total / data.size() << "\n";
return 0;
}
This produces slightly different output (i.e., minus the "running total") but I'm guessing you only added that for debugging.